From 9951662ebbd44a1f5f6716cd5fd1cabea71ceac8 Mon Sep 17 00:00:00 2001 From: TimothyBlynJacobs Date: Sat, 26 Sep 2020 18:20:07 +0000 Subject: [PATCH] REST API: Support the minProperties and maxProperties JSON Schema keywords. Props yakimun. Fixes #51023. Built from https://develop.svn.wordpress.org/trunk@49053 git-svn-id: http://core.svn.wordpress.org/trunk@48815 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/rest-api.php | 13 +++++++++++++ wp-includes/version.php | 2 +- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/wp-includes/rest-api.php b/wp-includes/rest-api.php index cbad102bbe..28f2ad6b73 100644 --- a/wp-includes/rest-api.php +++ b/wp-includes/rest-api.php @@ -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', diff --git a/wp-includes/version.php b/wp-includes/version.php index 6951703004..d3a1033c3a 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.6-alpha-49052'; +$wp_version = '5.6-alpha-49053'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.