Make WordPress Core


Ignore:
Timestamp:
10/04/2017 08:26:44 AM (9 years ago)
Author:
joehoyle
Message:

REST API: Support for objects in schema validation and sanitization.

When registering routes developers can now define their complex objects in the schema and benefit from the automatic validation and sanitization in the REST API. This also paves the way for support for complex object registration via register_meta and register_setting.

See #38583.
Props TimothyBlynJacobs5.

File:
1 edited

Legend:

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

    r39896 r41727  
    182182        }
    183183
     184        public function test_type_object() {
     185                $schema = array(
     186                        'type'       => 'object',
     187                        'properties' => array(
     188                                'a' => array(
     189                                        'type' => 'number'
     190                                ),
     191                        ),
     192                );
     193                $this->assertTrue( rest_validate_value_from_schema( array( 'a' => 1 ), $schema ) );
     194                $this->assertWPError( rest_validate_value_from_schema( array( 'a' => 'invalid' ), $schema ) );
     195        }
     196
     197        public function test_type_object_nested() {
     198                $schema = array(
     199                        'type' => 'object',
     200                        'properties' => array(
     201                                'a' => array(
     202                                        'type'  => 'object',
     203                                        'properties' => array(
     204                                                'b' => array( 'type' => 'number' ),
     205                                                'c' => array( 'type' => 'number' ),
     206                                        )
     207                                )
     208                        ),
     209                );
     210                $this->assertTrue(
     211                        rest_validate_value_from_schema(
     212                                array(
     213                                        'a' => array(
     214                                                'b' => '1',
     215                                                'c' => 3,
     216                                        ),
     217                                ),
     218                                $schema
     219                        )
     220                );
     221                $this->assertWPError( rest_validate_value_from_schema( array( 'a' => array( 'b' => 1, 'c' => 'invalid' ) ), $schema ) );
     222                $this->assertWPError( rest_validate_value_from_schema( array( 'a' => 1 ), $schema ) );
     223        }
     224
     225        public function test_type_object_stdclass() {
     226                $schema = array(
     227                        'type'       => 'object',
     228                        'properties' => array(
     229                                'a' => array(
     230                                        'type' => 'number'
     231                                ),
     232                        ),
     233                );
     234                $this->assertTrue( rest_validate_value_from_schema( (object) array( 'a' => 1 ), $schema ) );
     235        }
     236
    184237        public function test_type_unknown() {
    185238                $schema = array(
Note: See TracChangeset for help on using the changeset viewer.