From 352cb55ccab0f23ce611eaf5ce18e22d950990cf Mon Sep 17 00:00:00 2001 From: TimothyBlynJacobs Date: Mon, 27 Apr 2020 02:29:07 +0000 Subject: [PATCH] REST API: Support the (min|max)Length JSON Schema keywords. Props sorenbronsted. Fixes #48820. Built from https://develop.svn.wordpress.org/trunk@47627 git-svn-id: http://core.svn.wordpress.org/trunk@47402 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/rest-api.php | 32 +++++++++++++++++++++++++++++--- wp-includes/version.php | 2 +- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/wp-includes/rest-api.php b/wp-includes/rest-api.php index d807b15720..64d42cf0c0 100644 --- a/wp-includes/rest-api.php +++ b/wp-includes/rest-api.php @@ -1337,9 +1337,35 @@ function rest_validate_value_from_schema( $value, $args, $param = '' ) { return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s is not of type %2$s.' ), $param, 'boolean' ) ); } - if ( 'string' === $args['type'] && ! is_string( $value ) ) { - /* translators: 1: Parameter, 2: Type name. */ - return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s is not of type %2$s.' ), $param, 'string' ) ); + if ( 'string' === $args['type'] ) { + if ( ! is_string( $value ) ) { + /* translators: 1: Parameter, 2: Type name. */ + return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s is not of type %2$s.' ), $param, 'string' ) ); + } + + if ( isset( $args['minLength'] ) && mb_strlen( $value ) < $args['minLength'] ) { + return new WP_Error( + 'rest_invalid_param', + sprintf( + /* translators: 1: Parameter, 2: Number of characters. */ + _n( '%1$s must be at least %2$s character long.', '%1$s must be at least %2$s characters long.', $args['minLength'] ), + $param, + number_format_i18n( $args['minLength'] ) + ) + ); + } + + if ( isset( $args['maxLength'] ) && mb_strlen( $value ) > $args['maxLength'] ) { + return new WP_Error( + 'rest_invalid_param', + sprintf( + /* translators: 1: Parameter, 2: Number of characters. */ + _n( '%1$s must be at most %2$s character long.', '%1$s must be at most %2$s characters long.', $args['maxLength'] ), + $param, + number_format_i18n( $args['maxLength'] ) + ) + ); + } } if ( isset( $args['format'] ) ) { diff --git a/wp-includes/version.php b/wp-includes/version.php index ea0dc24566..78848c6315 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.5-alpha-47626'; +$wp_version = '5.5-alpha-47627'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.