Changeset 47923
- Timestamp:
- 06/07/2020 10:40:16 PM (5 years ago)
- Location:
- trunk
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/rest-api.php
r47849 r47923 1251 1251 * Support the "minLength", "maxLength" and "pattern" keywords for strings. 1252 1252 * Validate required properties. 1253 * Support the "minItems" and "maxItems" keywords for arrays. 1253 1254 * 1254 1255 * @param mixed $value The value to validate. … … 1287 1288 return $is_valid; 1288 1289 } 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'] ) ) ); 1289 1300 } 1290 1301 } -
trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-controller.php
r47911 r47923 644 644 'maxLength', 645 645 'pattern', 646 'minItems', 647 'maxItems', 646 648 ); 647 649 -
trunk/tests/phpunit/tests/rest-api/rest-controller.php
r47913 r47923 265 265 266 266 $this->assertArrayHasKey( 'items', $args['somearray'] ); 267 268 foreach ( array( 'minItems', 'maxItems' ) as $property ) { 269 $this->assertArrayHasKey( $property, $args['somearray'] ); 270 } 267 271 268 272 foreach ( array( 'properties', 'additionalProperties' ) as $property ) { -
trunk/tests/phpunit/tests/rest-api/rest-schema-validation.php
r47810 r47923 767 767 $this->assertWPError( rest_validate_value_from_schema( 'ââ', $schema ) ); 768 768 } 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 } 769 805 } -
trunk/tests/phpunit/tests/rest-api/rest-test-controller.php
r47911 r47923 102 102 ), 103 103 'somearray' => array( 104 'type' => 'array',105 'items' => array(104 'type' => 'array', 105 'items' => array( 106 106 'type' => 'string', 107 107 ), 108 'context' => array( 'view' ), 108 'minItems' => 1, 109 'maxItems' => 10, 110 'context' => array( 'view' ), 109 111 ), 110 112 'someobject' => array(
Note: See TracChangeset
for help on using the changeset viewer.