Make WordPress Core

Changeset 47810


Ignore:
Timestamp:
05/16/2020 07:01:49 PM (6 years ago)
Author:
TimothyBlynJacobs
Message:

REST API: Support the JSON Schema pattern keyword.

Props jason_the_adams, birgire, sorenbronsted.
Fixes #44949.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api.php

    r47809 r47810  
    13821382                                )
    13831383                        );
     1384                }
     1385
     1386                if ( isset( $args['pattern'] ) ) {
     1387                        $pattern = str_replace( '#', '\\#', $args['pattern'] );
     1388                        if ( ! preg_match( '#' . $pattern . '#u', $value ) ) {
     1389                                /* translators: 1: Parameter, 2: Pattern. */
     1390                                return new WP_Error( 'rest_invalid_pattern', sprintf( __( '%1$s does not match pattern %2$s.' ), $param, $args['pattern'] ) );
     1391                        }
    13841392                }
    13851393        }
  • trunk/tests/phpunit/tests/rest-api/rest-schema-validation.php

    r47809 r47810  
    728728                );
    729729        }
     730
     731        /**
     732         * @ticket 44949
     733         */
     734        public function test_string_pattern() {
     735                $schema = array(
     736                        'type'    => 'string',
     737                        'pattern' => '^a*$',
     738                );
     739
     740                $this->assertTrue( rest_validate_value_from_schema( 'a', $schema ) );
     741                $this->assertWPError( rest_validate_value_from_schema( 'b', $schema ) );
     742        }
     743
     744        /**
     745         * @ticket 44949
     746         */
     747        public function test_string_pattern_with_escaped_delimiter() {
     748                $schema = array(
     749                        'type'    => 'string',
     750                        'pattern' => '#[0-9]+',
     751                );
     752
     753                $this->assertTrue( rest_validate_value_from_schema( '#123', $schema ) );
     754                $this->assertWPError( rest_validate_value_from_schema( '#abc', $schema ) );
     755        }
     756
     757        /**
     758         * @ticket 44949
     759         */
     760        public function test_string_pattern_with_utf8() {
     761                $schema = array(
     762                        'type'    => 'string',
     763                        'pattern' => '^â{1}$',
     764                );
     765
     766                $this->assertTrue( rest_validate_value_from_schema( 'â', $schema ) );
     767                $this->assertWPError( rest_validate_value_from_schema( 'ââ', $schema ) );
     768        }
    730769}
Note: See TracChangeset for help on using the changeset viewer.