Make WordPress Core


Ignore:
Timestamp:
10/01/2020 02:47:08 AM (4 years ago)
Author:
TimothyBlynJacobs
Message:

REST API: Support the patternProperties JSON Schema keyword.

Props yakimun.
Fixes #51024.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/rest-schema-validation.php

    r49063 r49082  
    287287        );
    288288        $this->assertWPError( rest_validate_value_from_schema( array( 'a' => 'invalid' ), $schema ) );
     289    }
     290
     291    /**
     292     * @ticket 51024
     293     *
     294     * @dataProvider data_type_object_pattern_properties
     295     *
     296     * @param array $pattern_properties
     297     * @param array $value
     298     * @param bool $expected
     299     */
     300    public function test_type_object_pattern_properties( $pattern_properties, $value, $expected ) {
     301        $schema = array(
     302            'type'                 => 'object',
     303            'properties'           => array(
     304                'propA' => array( 'type' => 'string' ),
     305            ),
     306            'patternProperties'    => $pattern_properties,
     307            'additionalProperties' => false,
     308        );
     309
     310        if ( $expected ) {
     311            $this->assertTrue( rest_validate_value_from_schema( $value, $schema ) );
     312        } else {
     313            $this->assertWPError( rest_validate_value_from_schema( $value, $schema ) );
     314        }
     315    }
     316
     317    /**
     318     * @return array
     319     */
     320    public function data_type_object_pattern_properties() {
     321        return array(
     322            array( array(), array(), true ),
     323            array( array(), array( 'propA' => 'a' ), true ),
     324            array(
     325                array(),
     326                array(
     327                    'propA' => 'a',
     328                    'propB' => 'b',
     329                ),
     330                false,
     331            ),
     332            array(
     333                array(
     334                    'propB' => array( 'type' => 'string' ),
     335                ),
     336                array( 'propA' => 'a' ),
     337                true,
     338            ),
     339            array(
     340                array(
     341                    'propB' => array( 'type' => 'string' ),
     342                ),
     343                array(
     344                    'propA' => 'a',
     345                    'propB' => 'b',
     346                ),
     347                true,
     348            ),
     349            array(
     350                array(
     351                    '.*C' => array( 'type' => 'string' ),
     352                ),
     353                array(
     354                    'propA' => 'a',
     355                    'propC' => 'c',
     356                ),
     357                true,
     358            ),
     359            array(
     360                array(
     361                    '[0-9]' => array( 'type' => 'integer' ),
     362                ),
     363                array(
     364                    'propA' => 'a',
     365                    'prop0' => 0,
     366                ),
     367                true,
     368            ),
     369            array(
     370                array(
     371                    '[0-9]' => array( 'type' => 'integer' ),
     372                ),
     373                array(
     374                    'propA' => 'a',
     375                    'prop0' => 'notAnInteger',
     376                ),
     377                false,
     378            ),
     379            array(
     380                array(
     381                    '.+' => array( 'type' => 'string' ),
     382                ),
     383                array(
     384                    ''      => '',
     385                    'propA' => 'a',
     386                ),
     387                false,
     388            ),
     389        );
    289390    }
    290391
Note: See TracChangeset for help on using the changeset viewer.