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

A future commit will add support for the uniqueItems keyword.

Props sorenbronsted.
See #48821.


git-svn-id: https://develop.svn.wordpress.org/trunk@47923 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Timothy Jacobs
2020-06-07 22:40:16 +00:00
parent d3387a2161
commit f5dde31290
5 changed files with 58 additions and 3 deletions
+11
View File
@@ -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'] ) {