REST API: Support the uuid JSON Schema format.
This accepts a uuid of any version. A future commit could add support for restricting uuids to a specific version. Props johnwatkins0. Fixes #50053. git-svn-id: https://develop.svn.wordpress.org/trunk@47753 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
d8371d2134
commit
ef05acdfaa
@ -1393,6 +1393,12 @@ function rest_validate_value_from_schema( $value, $args, $param = '' ) {
|
||||
return new WP_Error( 'rest_invalid_param', sprintf( __( '%s is not a valid IP address.' ), $param ) );
|
||||
}
|
||||
break;
|
||||
case 'uuid':
|
||||
if ( ! wp_is_uuid( $value ) ) {
|
||||
/* translators: %s is the name of a JSON field expecting a valid uuid. */
|
||||
return new WP_Error( 'rest_invalid_uuid', sprintf( __( '%s is not a valid UUID.' ), $param ) );
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1549,6 +1555,9 @@ function rest_sanitize_value_from_schema( $value, $args ) {
|
||||
|
||||
case 'ip':
|
||||
return sanitize_text_field( $value );
|
||||
|
||||
case 'uuid':
|
||||
return sanitize_text_field( $value );
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -43,6 +43,10 @@ class WP_Test_REST_Controller extends WP_Test_REST_TestCase {
|
||||
'type' => 'string',
|
||||
'format' => 'email',
|
||||
),
|
||||
'someuuid' => array(
|
||||
'type' => 'string',
|
||||
'format' => 'uuid',
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
@ -204,6 +208,20 @@ class WP_Test_REST_Controller extends WP_Test_REST_TestCase {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 50053
|
||||
*/
|
||||
public function test_validate_schema_format_uuid() {
|
||||
$this->assertTrue(
|
||||
rest_validate_request_arg( '123e4567-e89b-12d3-a456-426655440000', $this->request, 'someuuid' )
|
||||
);
|
||||
|
||||
$this->assertErrorResponse(
|
||||
'rest_invalid_uuid',
|
||||
rest_validate_request_arg( '123e4567-e89b-12d3-a456-426655440000X', $this->request, 'someuuid' )
|
||||
);
|
||||
}
|
||||
|
||||
public function test_get_endpoint_args_for_item_schema_description() {
|
||||
$controller = new WP_REST_Test_Controller();
|
||||
$args = $controller->get_endpoint_args_for_item_schema();
|
||||
@ -245,6 +263,7 @@ class WP_Test_REST_Controller extends WP_Test_REST_TestCase {
|
||||
'somedate',
|
||||
'someemail',
|
||||
'somehex',
|
||||
'someuuid',
|
||||
'someenum',
|
||||
'someargoptions',
|
||||
'somedefault',
|
||||
@ -275,6 +294,7 @@ class WP_Test_REST_Controller extends WP_Test_REST_TestCase {
|
||||
'somedate',
|
||||
'someemail',
|
||||
'somehex',
|
||||
'someuuid',
|
||||
'someenum',
|
||||
'someargoptions',
|
||||
'somedefault',
|
||||
|
@ -90,6 +90,22 @@ class WP_Test_REST_Schema_Sanitization extends WP_UnitTestCase {
|
||||
$this->assertEquals( '', rest_sanitize_value_from_schema( 'WordPress', $schema ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 50053
|
||||
*/
|
||||
public function test_format_uuid() {
|
||||
$schema = array(
|
||||
'type' => 'string',
|
||||
'format' => 'uuid',
|
||||
);
|
||||
$this->assertEquals( '44', rest_sanitize_value_from_schema( 44, $schema ) );
|
||||
$this->assertEquals( 'hello', rest_sanitize_value_from_schema( 'hello', $schema ) );
|
||||
$this->assertEquals(
|
||||
'123e4567-e89b-12d3-a456-426655440000',
|
||||
rest_sanitize_value_from_schema( '123e4567-e89b-12d3-a456-426655440000', $schema )
|
||||
);
|
||||
}
|
||||
|
||||
public function test_type_array() {
|
||||
$schema = array(
|
||||
'type' => 'array',
|
||||
|
@ -85,6 +85,19 @@ class WP_Test_REST_Schema_Validation extends WP_UnitTestCase {
|
||||
$this->assertWPError( rest_validate_value_from_schema( 'WordPress', $schema ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 50053
|
||||
*/
|
||||
public function test_format_uuid() {
|
||||
$schema = array(
|
||||
'type' => 'string',
|
||||
'format' => 'uuid',
|
||||
);
|
||||
$this->assertTrue( rest_validate_value_from_schema( '123e4567-e89b-12d3-a456-426655440000', $schema ) );
|
||||
$this->assertWPError( rest_validate_value_from_schema( '123e4567-e89b-12d3-a456-426655440000X', $schema ) );
|
||||
$this->assertWPError( rest_validate_value_from_schema( '123e4567-e89b-?2d3-a456-426655440000', $schema ) );
|
||||
}
|
||||
|
||||
public function test_format_date_time() {
|
||||
$schema = array(
|
||||
'type' => 'string',
|
||||
|
@ -69,6 +69,11 @@ class WP_REST_Test_Controller extends WP_REST_Controller {
|
||||
'format' => 'hex-color',
|
||||
'context' => array( 'view' ),
|
||||
),
|
||||
'someuuid' => array(
|
||||
'type' => 'string',
|
||||
'format' => 'uuid',
|
||||
'context' => array( 'view' ),
|
||||
),
|
||||
'someenum' => array(
|
||||
'type' => 'string',
|
||||
'enum' => array( 'a', 'b', 'c' ),
|
||||
|
Loading…
x
Reference in New Issue
Block a user