Make WordPress Core


Ignore:
Timestamp:
10/04/2017 08:26:44 AM (7 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-sanitization.php

    r39296 r41727  
    147147    }
    148148
     149    public function test_type_object() {
     150        $schema = array(
     151            'type'       => 'object',
     152            'properties' => array(
     153                'a' => array(
     154                    'type' => 'number'
     155                ),
     156            ),
     157        );
     158        $this->assertEquals( array( 'a' => 1 ), rest_sanitize_value_from_schema( array( 'a' => 1 ), $schema ) );
     159        $this->assertEquals( array( 'a' => 1 ), rest_sanitize_value_from_schema( array( 'a' => '1' ), $schema ) );
     160    }
     161
     162    public function test_type_object_nested() {
     163        $schema = array(
     164            'type' => 'object',
     165            'properties' => array(
     166                'a' => array(
     167                    'type'  => 'object',
     168                    'properties' => array(
     169                        'b' => array( 'type' => 'number' ),
     170                        'c' => array( 'type' => 'number' ),
     171                    )
     172                )
     173            ),
     174        );
     175
     176        $this->assertEquals(
     177            array(
     178                'a' => array(
     179                    'b' => 1,
     180                    'c' => 3,
     181                ),
     182            ),
     183            rest_sanitize_value_from_schema(
     184                array(
     185                    'a' => array(
     186                        'b' => '1',
     187                        'c' => '3',
     188                    ),
     189                ),
     190                $schema
     191            )
     192        );
     193        $this->assertEquals(
     194            array(
     195                'a' => array(
     196                    'b' => 1,
     197                    'c' => 3,
     198                ),
     199            ),
     200            rest_sanitize_value_from_schema(
     201                array(
     202                    'a' => array(
     203                        'b' => '1',
     204                        'c' => '3',
     205                        'd' => '1',
     206                    ),
     207                    'b' => 1,
     208                ),
     209                $schema
     210            )
     211        );
     212        $this->assertEquals( array( 'a' => array() ), rest_sanitize_value_from_schema( array( 'a' => null ), $schema ) );
     213    }
     214
     215    public function test_type_object_stdclass() {
     216        $schema = array(
     217            'type'       => 'object',
     218            'properties' => array(
     219                'a' => array(
     220                    'type' => 'number'
     221                ),
     222            ),
     223        );
     224        $this->assertEquals( array( 'a' => 1 ), rest_sanitize_value_from_schema( (object) array( 'a' => '1' ), $schema ) );
     225    }
     226
    149227    public function test_type_unknown() {
    150228        $schema = array(
Note: See TracChangeset for help on using the changeset viewer.