From 5efaf888a5a52621350dacac79ea127c6481bbef Mon Sep 17 00:00:00 2001 From: TimothyBlynJacobs Date: Sun, 7 Jun 2020 22:42:13 +0000 Subject: [PATCH] REST API: Support the (min|max)Items JSON Schema keywords. A future commit will add support for the uniqueItems keyword. Props sorenbronsted. See #48821. Built from https://develop.svn.wordpress.org/trunk@47923 git-svn-id: http://core.svn.wordpress.org/trunk@47697 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/rest-api.php | 11 +++++++++++ .../rest-api/endpoints/class-wp-rest-controller.php | 2 ++ wp-includes/version.php | 2 +- 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/wp-includes/rest-api.php b/wp-includes/rest-api.php index e408522ab6..dc55af08f3 100644 --- a/wp-includes/rest-api.php +++ b/wp-includes/rest-api.php @@ -1250,6 +1250,7 @@ function rest_get_avatar_sizes() { * @since 5.5.0 Add the "uuid" and "hex-color" formats. * Support the "minLength", "maxLength" and "pattern" keywords for strings. * Validate required properties. + * Support the "minItems" and "maxItems" keywords for arrays. * * @param mixed $value The value to validate. * @param array $args Schema array to use for validation. @@ -1287,6 +1288,16 @@ function rest_validate_value_from_schema( $value, $args, $param = '' ) { return $is_valid; } } + + if ( isset( $args['minItems'] ) && count( $value ) < $args['minItems'] ) { + /* translators: 1: Parameter, 2: number. */ + return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s must contain at least %2$s items.' ), $param, number_format_i18n( $args['minItems'] ) ) ); + } + + if ( isset( $args['maxItems'] ) && count( $value ) > $args['maxItems'] ) { + /* translators: 1: Parameter, 2: number. */ + return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s must contain at most %2$s items.' ), $param, number_format_i18n( $args['maxItems'] ) ) ); + } } if ( 'object' === $args['type'] ) { diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-controller.php index a2d1134f11..0d1e85cbf1 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-controller.php @@ -643,6 +643,8 @@ abstract class WP_REST_Controller { 'minLength', 'maxLength', 'pattern', + 'minItems', + 'maxItems', ); foreach ( $schema_properties as $field_id => $params ) { diff --git a/wp-includes/version.php b/wp-includes/version.php index b264bea2b0..94cb1e9791 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.5-alpha-47922'; +$wp_version = '5.5-alpha-47923'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.