Make WordPress Core


Ignore:
Timestamp:
11/14/2016 04:35:35 PM (8 years ago)
Author:
joehoyle
Message:

REST API: Validate and Sanitize registered meta based off the schema.

With the addition of Array support in our schema validation functions, it's now possible to use these in the meta validation and sanitization steps. Also, this increases the test coverage of using registered via meta the API significantly.

Fixes #38531.
Props rachelbaker, tharsheblows.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/rest-schema-validation.php

    r39159 r39222  
    105105    }
    106106
     107    public function test_type_array_nested() {
     108        $schema = array(
     109            'type' => 'array',
     110            'items' => array(
     111                'type' => 'array',
     112                'items' => array(
     113                    'type' => 'number',
     114                ),
     115            ),
     116        );
     117        $this->assertTrue( rest_validate_value_from_schema( array( array( 1 ), array( 2 ) ), $schema ) );
     118    }
     119
    107120    public function test_type_array_as_csv() {
    108121        $schema = array(
     
    140153        $this->assertWPError( rest_validate_value_from_schema( 'chicken,coleslaw', $schema ) );
    141154    }
     155
     156    public function test_type_array_is_associative() {
     157        $schema = array(
     158            'type'  => 'array',
     159            'items' => array(
     160                'type' => 'string',
     161            ),
     162        );
     163        $this->assertWPError( rest_validate_value_from_schema( array( 'first' => '1', 'second' => '2' ), $schema ) );
     164    }
     165
     166    public function test_type_unknown() {
     167        $schema = array(
     168            'type'  => 'lalala',
     169        );
     170        $this->assertTrue( rest_validate_value_from_schema( 'Best lyrics', $schema ) );
     171        $this->assertTrue( rest_validate_value_from_schema( 1, $schema ) );
     172        $this->assertTrue( rest_validate_value_from_schema( array(), $schema ) );
     173    }
    142174}
Note: See TracChangeset for help on using the changeset viewer.