diff --git src/wp-includes/rest-api.php src/wp-includes/rest-api.php
index 870bc6443c..fe111c92e6 100644
|
|
|
function rest_get_allowed_schema_keywords() { |
| 1908 | 1908 | 'uniqueItems', |
| 1909 | 1909 | 'anyOf', |
| 1910 | 1910 | 'oneOf', |
| | 1911 | 'const', |
| 1911 | 1912 | ); |
| 1912 | 1913 | } |
| 1913 | 1914 | |
| … |
… |
function rest_validate_value_from_schema( $value, $args, $param = '' ) { |
| 2160 | 2161 | } |
| 2161 | 2162 | } |
| 2162 | 2163 | |
| | 2164 | if ( ! empty( $args['const'] ) ) { |
| | 2165 | if ( $value !== $args['const'] ) { |
| | 2166 | /* translators: 1: Parameter, 2: The valid value. */ |
| | 2167 | return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s is not const %2$s.' ), $param, $args['const'] ) ); |
| | 2168 | } |
| | 2169 | } |
| | 2170 | |
| 2163 | 2171 | if ( in_array( $args['type'], array( 'integer', 'number' ), true ) ) { |
| 2164 | 2172 | if ( ! is_numeric( $value ) ) { |
| 2165 | 2173 | return new WP_Error( |
diff --git tests/phpunit/tests/rest-api/rest-schema-validation.php tests/phpunit/tests/rest-api/rest-schema-validation.php
index f33990ae59..c9cf4be7ed 100644
|
|
|
class WP_Test_REST_Schema_Validation extends WP_UnitTestCase { |
| 62 | 62 | $this->assertWPError( rest_validate_value_from_schema( 1123, $schema ) ); |
| 63 | 63 | } |
| 64 | 64 | |
| | 65 | /** |
| | 66 | * @ticket 51757 |
| | 67 | */ |
| | 68 | public function test_const() { |
| | 69 | $schema = array( |
| | 70 | 'type' => 'string', |
| | 71 | 'const' => 'ananas', |
| | 72 | ); |
| | 73 | $valid = 'ananas'; |
| | 74 | $invalid = 'pineapple'; |
| | 75 | |
| | 76 | $this->assertTrue( rest_validate_value_from_schema( $valid, $schema ) ); |
| | 77 | $this->assertWPError( rest_validate_value_from_schema( $invalid, $schema ) ); |
| | 78 | |
| | 79 | // By definition, const is functionally equivalent to an enum with a single value. |
| | 80 | // However, they produce different error messages, so the invalid isn't tested. |
| | 81 | $enum_schema = array( |
| | 82 | 'type' => 'string', |
| | 83 | 'enum' => array( 'ananas' ), |
| | 84 | ); |
| | 85 | $this->assertSame( |
| | 86 | rest_validate_value_from_schema( $valid, $schema ), |
| | 87 | rest_validate_value_from_schema( $valid, $enum_schema ) |
| | 88 | ); |
| | 89 | } |
| | 90 | |
| 65 | 91 | public function test_format_email() { |
| 66 | 92 | $schema = array( |
| 67 | 93 | 'type' => 'string', |