REST API: Support the minProperties and maxProperties JSON Schema keywords.

Props yakimun.
Fixes #51023.


git-svn-id: https://develop.svn.wordpress.org/trunk@49053 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Timothy Jacobs
2020-09-26 18:18:53 +00:00
parent 9aa0e0c92b
commit d5c2c86bd8
4 changed files with 71 additions and 1 deletions

View File

@@ -1551,6 +1551,7 @@ function rest_stabilize_value( $value ) {
* Support the "minLength", "maxLength" and "pattern" keywords for strings.
* Support the "minItems", "maxItems" and "uniqueItems" keywords for arrays.
* Validate required properties.
* @since 5.7.0 Support the "minProperties" and "maxProperties" keywords for objects.
*
* @param mixed $value The value to validate.
* @param array $args Schema array to use for validation.
@@ -1662,6 +1663,16 @@ function rest_validate_value_from_schema( $value, $args, $param = '' ) {
}
}
}
if ( isset( $args['minProperties'] ) && count( $value ) < $args['minProperties'] ) {
/* translators: 1: Parameter, 2: Number. */
return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s must contain at least %2$s properties.' ), $param, number_format_i18n( $args['minProperties'] ) ) );
}
if ( isset( $args['maxProperties'] ) && count( $value ) > $args['maxProperties'] ) {
/* translators: 1: Parameter, 2: Number. */
return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s must contain at most %2$s properties.' ), $param, number_format_i18n( $args['maxProperties'] ) ) );
}
}
if ( 'null' === $args['type'] ) {
@@ -2281,6 +2292,8 @@ function rest_get_endpoint_args_for_schema( $schema, $method = WP_REST_Server::C
'items',
'properties',
'additionalProperties',
'minProperties',
'maxProperties',
'minimum',
'maximum',
'exclusiveMinimum',