mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-06-28 14:20:15 +00:00
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. git-svn-id: https://develop.svn.wordpress.org/trunk@41727 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -146,6 +146,84 @@ class WP_Test_REST_Schema_Sanitization extends WP_UnitTestCase {
|
||||
$this->assertEquals( array( '1', '2' ), rest_sanitize_value_from_schema( array( 'first' => '1', 'second' => '2' ), $schema ) );
|
||||
}
|
||||
|
||||
public function test_type_object() {
|
||||
$schema = array(
|
||||
'type' => 'object',
|
||||
'properties' => array(
|
||||
'a' => array(
|
||||
'type' => 'number'
|
||||
),
|
||||
),
|
||||
);
|
||||
$this->assertEquals( array( 'a' => 1 ), rest_sanitize_value_from_schema( array( 'a' => 1 ), $schema ) );
|
||||
$this->assertEquals( array( 'a' => 1 ), rest_sanitize_value_from_schema( array( 'a' => '1' ), $schema ) );
|
||||
}
|
||||
|
||||
public function test_type_object_nested() {
|
||||
$schema = array(
|
||||
'type' => 'object',
|
||||
'properties' => array(
|
||||
'a' => array(
|
||||
'type' => 'object',
|
||||
'properties' => array(
|
||||
'b' => array( 'type' => 'number' ),
|
||||
'c' => array( 'type' => 'number' ),
|
||||
)
|
||||
)
|
||||
),
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
array(
|
||||
'a' => array(
|
||||
'b' => 1,
|
||||
'c' => 3,
|
||||
),
|
||||
),
|
||||
rest_sanitize_value_from_schema(
|
||||
array(
|
||||
'a' => array(
|
||||
'b' => '1',
|
||||
'c' => '3',
|
||||
),
|
||||
),
|
||||
$schema
|
||||
)
|
||||
);
|
||||
$this->assertEquals(
|
||||
array(
|
||||
'a' => array(
|
||||
'b' => 1,
|
||||
'c' => 3,
|
||||
),
|
||||
),
|
||||
rest_sanitize_value_from_schema(
|
||||
array(
|
||||
'a' => array(
|
||||
'b' => '1',
|
||||
'c' => '3',
|
||||
'd' => '1',
|
||||
),
|
||||
'b' => 1,
|
||||
),
|
||||
$schema
|
||||
)
|
||||
);
|
||||
$this->assertEquals( array( 'a' => array() ), rest_sanitize_value_from_schema( array( 'a' => null ), $schema ) );
|
||||
}
|
||||
|
||||
public function test_type_object_stdclass() {
|
||||
$schema = array(
|
||||
'type' => 'object',
|
||||
'properties' => array(
|
||||
'a' => array(
|
||||
'type' => 'number'
|
||||
),
|
||||
),
|
||||
);
|
||||
$this->assertEquals( array( 'a' => 1 ), rest_sanitize_value_from_schema( (object) array( 'a' => '1' ), $schema ) );
|
||||
}
|
||||
|
||||
public function test_type_unknown() {
|
||||
$schema = array(
|
||||
'type' => 'lalala',
|
||||
|
||||
@@ -181,6 +181,59 @@ class WP_Test_REST_Schema_Validation extends WP_UnitTestCase {
|
||||
$this->assertWPError( rest_validate_value_from_schema( array( 'first' => '1', 'second' => '2' ), $schema ) );
|
||||
}
|
||||
|
||||
public function test_type_object() {
|
||||
$schema = array(
|
||||
'type' => 'object',
|
||||
'properties' => array(
|
||||
'a' => array(
|
||||
'type' => 'number'
|
||||
),
|
||||
),
|
||||
);
|
||||
$this->assertTrue( rest_validate_value_from_schema( array( 'a' => 1 ), $schema ) );
|
||||
$this->assertWPError( rest_validate_value_from_schema( array( 'a' => 'invalid' ), $schema ) );
|
||||
}
|
||||
|
||||
public function test_type_object_nested() {
|
||||
$schema = array(
|
||||
'type' => 'object',
|
||||
'properties' => array(
|
||||
'a' => array(
|
||||
'type' => 'object',
|
||||
'properties' => array(
|
||||
'b' => array( 'type' => 'number' ),
|
||||
'c' => array( 'type' => 'number' ),
|
||||
)
|
||||
)
|
||||
),
|
||||
);
|
||||
$this->assertTrue(
|
||||
rest_validate_value_from_schema(
|
||||
array(
|
||||
'a' => array(
|
||||
'b' => '1',
|
||||
'c' => 3,
|
||||
),
|
||||
),
|
||||
$schema
|
||||
)
|
||||
);
|
||||
$this->assertWPError( rest_validate_value_from_schema( array( 'a' => array( 'b' => 1, 'c' => 'invalid' ) ), $schema ) );
|
||||
$this->assertWPError( rest_validate_value_from_schema( array( 'a' => 1 ), $schema ) );
|
||||
}
|
||||
|
||||
public function test_type_object_stdclass() {
|
||||
$schema = array(
|
||||
'type' => 'object',
|
||||
'properties' => array(
|
||||
'a' => array(
|
||||
'type' => 'number'
|
||||
),
|
||||
),
|
||||
);
|
||||
$this->assertTrue( rest_validate_value_from_schema( (object) array( 'a' => 1 ), $schema ) );
|
||||
}
|
||||
|
||||
public function test_type_unknown() {
|
||||
$schema = array(
|
||||
'type' => 'lalala',
|
||||
|
||||
Reference in New Issue
Block a user