mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-08-11 12:20:22 +00:00
REST API: Support the patternProperties JSON Schema keyword.
Props yakimun. Fixes #51024. git-svn-id: https://develop.svn.wordpress.org/trunk@49082 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -1284,6 +1284,42 @@ class Tests_REST_API extends WP_UnitTestCase {
|
||||
),
|
||||
array( 'additional' => array( 'a' => '1' ) ),
|
||||
),
|
||||
'pattern properties' => array(
|
||||
array(
|
||||
'$schema' => 'http://json-schema.org/draft-04/schema#',
|
||||
'type' => 'object',
|
||||
'properties' => array(
|
||||
'a' => array(
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
),
|
||||
'patternProperties' => array(
|
||||
'[0-9]' => array(
|
||||
'type' => 'string',
|
||||
'context' => array( 'view', 'edit' ),
|
||||
),
|
||||
'c.*' => array(
|
||||
'type' => 'string',
|
||||
'context' => array( 'edit' ),
|
||||
),
|
||||
),
|
||||
'additionalProperties' => array(
|
||||
'type' => 'string',
|
||||
'context' => array( 'edit' ),
|
||||
),
|
||||
),
|
||||
array(
|
||||
'a' => '1',
|
||||
'b' => '2',
|
||||
'0' => '3',
|
||||
'ca' => '4',
|
||||
),
|
||||
array(
|
||||
'a' => '1',
|
||||
'0' => '3',
|
||||
),
|
||||
),
|
||||
'multiple types object' => array(
|
||||
array(
|
||||
'$schema' => 'http://json-schema.org/draft-04/schema#',
|
||||
|
||||
@@ -291,7 +291,14 @@ class WP_Test_REST_Controller extends WP_Test_REST_TestCase {
|
||||
$this->assertArrayHasKey( $property, $args['somearray'] );
|
||||
}
|
||||
|
||||
foreach ( array( 'properties', 'additionalProperties', 'minProperties', 'maxProperties' ) as $property ) {
|
||||
$object_properties = array(
|
||||
'properties',
|
||||
'patternProperties',
|
||||
'additionalProperties',
|
||||
'minProperties',
|
||||
'maxProperties',
|
||||
);
|
||||
foreach ( $object_properties as $property ) {
|
||||
$this->assertArrayHasKey( $property, $args['someobject'] );
|
||||
}
|
||||
|
||||
|
||||
@@ -237,6 +237,102 @@ class WP_Test_REST_Schema_Sanitization extends WP_UnitTestCase {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 51024
|
||||
*
|
||||
* @dataProvider data_type_object_pattern_properties
|
||||
*
|
||||
* @param array $pattern_properties
|
||||
* @param array $value
|
||||
* @param array $expected
|
||||
*/
|
||||
public function test_type_object_pattern_properties( $pattern_properties, $value, $expected ) {
|
||||
$schema = array(
|
||||
'type' => 'object',
|
||||
'properties' => array(
|
||||
'propA' => array( 'type' => 'string' ),
|
||||
),
|
||||
'patternProperties' => $pattern_properties,
|
||||
'additionalProperties' => false,
|
||||
);
|
||||
|
||||
$this->assertSame( $expected, rest_sanitize_value_from_schema( $value, $schema ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function data_type_object_pattern_properties() {
|
||||
return array(
|
||||
array( array(), array(), array() ),
|
||||
array( array(), array( 'propA' => 'a' ), array( 'propA' => 'a' ) ),
|
||||
array(
|
||||
array(),
|
||||
array(
|
||||
'propA' => 'a',
|
||||
'propB' => 'b',
|
||||
),
|
||||
array( 'propA' => 'a' ),
|
||||
),
|
||||
array(
|
||||
array(
|
||||
'propB' => array( 'type' => 'string' ),
|
||||
),
|
||||
array( 'propA' => 'a' ),
|
||||
array( 'propA' => 'a' ),
|
||||
),
|
||||
array(
|
||||
array(
|
||||
'propB' => array( 'type' => 'string' ),
|
||||
),
|
||||
array(
|
||||
'propA' => 'a',
|
||||
'propB' => 'b',
|
||||
),
|
||||
array(
|
||||
'propA' => 'a',
|
||||
'propB' => 'b',
|
||||
),
|
||||
),
|
||||
array(
|
||||
array(
|
||||
'.*C' => array( 'type' => 'string' ),
|
||||
),
|
||||
array(
|
||||
'propA' => 'a',
|
||||
'propC' => 'c',
|
||||
),
|
||||
array(
|
||||
'propA' => 'a',
|
||||
'propC' => 'c',
|
||||
),
|
||||
),
|
||||
array(
|
||||
array(
|
||||
'[0-9]' => array( 'type' => 'integer' ),
|
||||
),
|
||||
array(
|
||||
'propA' => 'a',
|
||||
'prop0' => '0',
|
||||
),
|
||||
array(
|
||||
'propA' => 'a',
|
||||
'prop0' => 0,
|
||||
),
|
||||
),
|
||||
array(
|
||||
array(
|
||||
'.+' => array( 'type' => 'string' ),
|
||||
),
|
||||
array(
|
||||
'' => '',
|
||||
'propA' => 'a',
|
||||
),
|
||||
array( 'propA' => 'a' ),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
public function test_type_object_nested() {
|
||||
$schema = array(
|
||||
'type' => 'object',
|
||||
|
||||
@@ -288,6 +288,107 @@ class WP_Test_REST_Schema_Validation extends WP_UnitTestCase {
|
||||
$this->assertWPError( rest_validate_value_from_schema( array( 'a' => 'invalid' ), $schema ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 51024
|
||||
*
|
||||
* @dataProvider data_type_object_pattern_properties
|
||||
*
|
||||
* @param array $pattern_properties
|
||||
* @param array $value
|
||||
* @param bool $expected
|
||||
*/
|
||||
public function test_type_object_pattern_properties( $pattern_properties, $value, $expected ) {
|
||||
$schema = array(
|
||||
'type' => 'object',
|
||||
'properties' => array(
|
||||
'propA' => array( 'type' => 'string' ),
|
||||
),
|
||||
'patternProperties' => $pattern_properties,
|
||||
'additionalProperties' => false,
|
||||
);
|
||||
|
||||
if ( $expected ) {
|
||||
$this->assertTrue( rest_validate_value_from_schema( $value, $schema ) );
|
||||
} else {
|
||||
$this->assertWPError( rest_validate_value_from_schema( $value, $schema ) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function data_type_object_pattern_properties() {
|
||||
return array(
|
||||
array( array(), array(), true ),
|
||||
array( array(), array( 'propA' => 'a' ), true ),
|
||||
array(
|
||||
array(),
|
||||
array(
|
||||
'propA' => 'a',
|
||||
'propB' => 'b',
|
||||
),
|
||||
false,
|
||||
),
|
||||
array(
|
||||
array(
|
||||
'propB' => array( 'type' => 'string' ),
|
||||
),
|
||||
array( 'propA' => 'a' ),
|
||||
true,
|
||||
),
|
||||
array(
|
||||
array(
|
||||
'propB' => array( 'type' => 'string' ),
|
||||
),
|
||||
array(
|
||||
'propA' => 'a',
|
||||
'propB' => 'b',
|
||||
),
|
||||
true,
|
||||
),
|
||||
array(
|
||||
array(
|
||||
'.*C' => array( 'type' => 'string' ),
|
||||
),
|
||||
array(
|
||||
'propA' => 'a',
|
||||
'propC' => 'c',
|
||||
),
|
||||
true,
|
||||
),
|
||||
array(
|
||||
array(
|
||||
'[0-9]' => array( 'type' => 'integer' ),
|
||||
),
|
||||
array(
|
||||
'propA' => 'a',
|
||||
'prop0' => 0,
|
||||
),
|
||||
true,
|
||||
),
|
||||
array(
|
||||
array(
|
||||
'[0-9]' => array( 'type' => 'integer' ),
|
||||
),
|
||||
array(
|
||||
'propA' => 'a',
|
||||
'prop0' => 'notAnInteger',
|
||||
),
|
||||
false,
|
||||
),
|
||||
array(
|
||||
array(
|
||||
'.+' => array( 'type' => 'string' ),
|
||||
),
|
||||
array(
|
||||
'' => '',
|
||||
'propA' => 'a',
|
||||
),
|
||||
false,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
public function test_type_object_additional_properties_false() {
|
||||
$schema = array(
|
||||
'type' => 'object',
|
||||
|
||||
@@ -121,6 +121,11 @@ class WP_REST_Test_Controller extends WP_REST_Controller {
|
||||
'type' => 'integer',
|
||||
),
|
||||
),
|
||||
'patternProperties' => array(
|
||||
'[0-9]' => array(
|
||||
'type' => 'string',
|
||||
),
|
||||
),
|
||||
'minProperties' => 1,
|
||||
'maxProperties' => 10,
|
||||
'ignored_prop' => 'ignored_prop',
|
||||
|
||||
Reference in New Issue
Block a user