diff --git a/src/wp-includes/rest-api.php b/src/wp-includes/rest-api.php index 6c2e7b693b..cc600789f0 100644 --- a/src/wp-includes/rest-api.php +++ b/src/wp-includes/rest-api.php @@ -1382,6 +1382,14 @@ function rest_validate_value_from_schema( $value, $args, $param = '' ) { ) ); } + + if ( isset( $args['pattern'] ) ) { + $pattern = str_replace( '#', '\\#', $args['pattern'] ); + if ( ! preg_match( '#' . $pattern . '#u', $value ) ) { + /* translators: 1: Parameter, 2: Pattern. */ + return new WP_Error( 'rest_invalid_pattern', sprintf( __( '%1$s does not match pattern %2$s.' ), $param, $args['pattern'] ) ); + } + } } if ( isset( $args['format'] ) ) { diff --git a/tests/phpunit/tests/rest-api/rest-schema-validation.php b/tests/phpunit/tests/rest-api/rest-schema-validation.php index a46b1f0de0..09dd271a23 100644 --- a/tests/phpunit/tests/rest-api/rest-schema-validation.php +++ b/tests/phpunit/tests/rest-api/rest-schema-validation.php @@ -727,4 +727,43 @@ class WP_Test_REST_Schema_Validation extends WP_UnitTestCase { ), ); } + + /** + * @ticket 44949 + */ + public function test_string_pattern() { + $schema = array( + 'type' => 'string', + 'pattern' => '^a*$', + ); + + $this->assertTrue( rest_validate_value_from_schema( 'a', $schema ) ); + $this->assertWPError( rest_validate_value_from_schema( 'b', $schema ) ); + } + + /** + * @ticket 44949 + */ + public function test_string_pattern_with_escaped_delimiter() { + $schema = array( + 'type' => 'string', + 'pattern' => '#[0-9]+', + ); + + $this->assertTrue( rest_validate_value_from_schema( '#123', $schema ) ); + $this->assertWPError( rest_validate_value_from_schema( '#abc', $schema ) ); + } + + /** + * @ticket 44949 + */ + public function test_string_pattern_with_utf8() { + $schema = array( + 'type' => 'string', + 'pattern' => '^â{1}$', + ); + + $this->assertTrue( rest_validate_value_from_schema( 'â', $schema ) ); + $this->assertWPError( rest_validate_value_from_schema( 'ââ', $schema ) ); + } }