Make WordPress Core

Changeset 47923


Ignore:
Timestamp:
06/07/2020 10:40:16 PM (6 years ago)
Author:
TimothyBlynJacobs
Message:

REST API: Support the (min|max)Items JSON Schema keywords.

A future commit will add support for the uniqueItems keyword.

Props sorenbronsted.
See #48821.

Location:
trunk
Files:
5 edited

Legend:

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

    r47849 r47923  
    12511251 *              Support the "minLength", "maxLength" and "pattern" keywords for strings.
    12521252 *              Validate required properties.
     1253 *              Support the "minItems" and "maxItems" keywords for arrays.
    12531254 *
    12541255 * @param mixed  $value The value to validate.
     
    12871288                                return $is_valid;
    12881289                        }
     1290                }
     1291
     1292                if ( isset( $args['minItems'] ) && count( $value ) < $args['minItems'] ) {
     1293                        /* translators: 1: Parameter, 2: number. */
     1294                        return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s must contain at least %2$s items.' ), $param, number_format_i18n( $args['minItems'] ) ) );
     1295                }
     1296
     1297                if ( isset( $args['maxItems'] ) && count( $value ) > $args['maxItems'] ) {
     1298                        /* translators: 1: Parameter, 2: number. */
     1299                        return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s must contain at most %2$s items.' ), $param, number_format_i18n( $args['maxItems'] ) ) );
    12891300                }
    12901301        }
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-controller.php

    r47911 r47923  
    644644                        'maxLength',
    645645                        'pattern',
     646                        'minItems',
     647                        'maxItems',
    646648                );
    647649
  • trunk/tests/phpunit/tests/rest-api/rest-controller.php

    r47913 r47923  
    265265
    266266                $this->assertArrayHasKey( 'items', $args['somearray'] );
     267
     268                foreach ( array( 'minItems', 'maxItems' ) as $property ) {
     269                        $this->assertArrayHasKey( $property, $args['somearray'] );
     270                }
    267271
    268272                foreach ( array( 'properties', 'additionalProperties' ) as $property ) {
  • trunk/tests/phpunit/tests/rest-api/rest-schema-validation.php

    r47810 r47923  
    767767                $this->assertWPError( rest_validate_value_from_schema( 'ââ', $schema ) );
    768768        }
     769
     770        /**
     771         * @ticket 48821
     772         */
     773        public function test_array_min_items() {
     774                $schema = array(
     775                        'type'     => 'array',
     776                        'minItems' => 1,
     777                        'items'    => array(
     778                                'type' => 'number',
     779                        ),
     780                );
     781
     782                $this->assertTrue( rest_validate_value_from_schema( array( 1, 2 ), $schema ) );
     783                $this->assertTrue( rest_validate_value_from_schema( array( 1 ), $schema ) );
     784                $this->assertWPError( rest_validate_value_from_schema( array(), $schema ) );
     785                $this->assertWPError( rest_validate_value_from_schema( '', $schema ) );
     786        }
     787
     788        /**
     789         * @ticket 48821
     790         */
     791        public function test_array_max_items() {
     792                $schema = array(
     793                        'type'     => 'array',
     794                        'maxItems' => 2,
     795                        'items'    => array(
     796                                'type' => 'number',
     797                        ),
     798                );
     799
     800                $this->assertTrue( rest_validate_value_from_schema( array( 1 ), $schema ) );
     801                $this->assertTrue( rest_validate_value_from_schema( array( 1, 2 ), $schema ) );
     802                $this->assertWPError( rest_validate_value_from_schema( array( 1, 2, 3 ), $schema ) );
     803                $this->assertWPError( rest_validate_value_from_schema( 'foobar', $schema ) );
     804        }
    769805}
  • trunk/tests/phpunit/tests/rest-api/rest-test-controller.php

    r47911 r47923  
    102102                                ),
    103103                                'somearray'      => array(
    104                                         'type'    => 'array',
    105                                         'items'   => array(
     104                                        'type'     => 'array',
     105                                        'items'    => array(
    106106                                                'type' => 'string',
    107107                                        ),
    108                                         'context' => array( 'view' ),
     108                                        'minItems' => 1,
     109                                        'maxItems' => 10,
     110                                        'context'  => array( 'view' ),
    109111                                ),
    110112                                'someobject'     => array(
Note: See TracChangeset for help on using the changeset viewer.