<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[50060] trunk/src/wp-includes/rest-api.php: REST API: Refactor `rest_validate_value_from_schema` into separate validation functions per-type.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/50060">50060</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/50060","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>TimothyBlynJacobs</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2021-01-28 17:59:17 +0000 (Thu, 28 Jan 2021)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>REST API: Refactor `rest_validate_value_from_schema` into separate validation functions per-type.
Props yakimun.
Fixes <a href="https://core.trac.wordpress.org/ticket/52375">#52375</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesrestapiphp">trunk/src/wp-includes/rest-api.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesrestapiphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/rest-api.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/rest-api.php 2021-01-28 17:23:18 UTC (rev 50059)
+++ trunk/src/wp-includes/rest-api.php 2021-01-28 17:59:17 UTC (rev 50060)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2054,329 +2054,437 @@
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( 'array' === $args['type'] ) {
- if ( ! rest_is_array( $value ) ) {
- return new WP_Error(
- 'rest_invalid_type',
- /* translators: 1: Parameter, 2: Type name. */
- sprintf( __( '%1$s is not of type %2$s.' ), $param, 'array' ),
- array( 'param' => $param )
- );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ switch ( $args['type'] ) {
+ case 'null':
+ $is_valid = rest_validate_null_value_from_schema( $value, $param );
+ break;
+ case 'boolean':
+ $is_valid = rest_validate_boolean_value_from_schema( $value, $param );
+ break;
+ case 'object':
+ $is_valid = rest_validate_object_value_from_schema( $value, $args, $param );
+ break;
+ case 'array':
+ $is_valid = rest_validate_array_value_from_schema( $value, $args, $param );
+ break;
+ case 'number':
+ $is_valid = rest_validate_number_value_from_schema( $value, $args, $param );
+ break;
+ case 'string':
+ $is_valid = rest_validate_string_value_from_schema( $value, $args, $param );
+ break;
+ case 'integer':
+ $is_valid = rest_validate_integer_value_from_schema( $value, $args, $param );
+ break;
+ default:
+ $is_valid = true;
+ break;
+ }
+
+ if ( is_wp_error( $is_valid ) ) {
+ return $is_valid;
+ }
+
+ if ( ! empty( $args['enum'] ) ) {
+ $enum_contains_value = rest_validate_enum( $value, $args, $param );
+ if ( is_wp_error( $enum_contains_value ) ) {
+ return $enum_contains_value;
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $value = rest_sanitize_array( $value );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // The "format" keyword should only be applied to strings. However, for backward compatibility,
+ // we allow the "format" keyword if the type keyword was not specified, or was set to an invalid value.
+ if ( isset( $args['format'] )
+ && ( ! isset( $args['type'] ) || 'string' === $args['type'] || ! in_array( $args['type'], $allowed_types, true ) )
+ ) {
+ switch ( $args['format'] ) {
+ case 'hex-color':
+ if ( ! rest_parse_hex_color( $value ) ) {
+ return new WP_Error( 'rest_invalid_hex_color', __( 'Invalid hex color.' ) );
+ }
+ break;
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( isset( $args['items'] ) ) {
- foreach ( $value as $index => $v ) {
- $is_valid = rest_validate_value_from_schema( $v, $args['items'], $param . '[' . $index . ']' );
- if ( is_wp_error( $is_valid ) ) {
- return $is_valid;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ case 'date-time':
+ if ( ! rest_parse_date( $value ) ) {
+ return new WP_Error( 'rest_invalid_date', __( 'Invalid date.' ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- }
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ break;
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( isset( $args['minItems'] ) && count( $value ) < $args['minItems'] ) {
- return new WP_Error(
- 'rest_too_few_items',
- sprintf(
- /* translators: 1: Parameter, 2: Number. */
- _n(
- '%1$s must contain at least %2$s item.',
- '%1$s must contain at least %2$s items.',
- $args['minItems']
- ),
- $param,
- number_format_i18n( $args['minItems'] )
- )
- );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ case 'email':
+ if ( ! is_email( $value ) ) {
+ return new WP_Error( 'rest_invalid_email', __( 'Invalid email address.' ) );
+ }
+ break;
+ case 'ip':
+ if ( ! rest_is_ip_address( $value ) ) {
+ /* translators: %s: IP address. */
+ return new WP_Error( 'rest_invalid_ip', sprintf( __( '%s is not a valid IP address.' ), $param ) );
+ }
+ break;
+ case 'uuid':
+ if ( ! wp_is_uuid( $value ) ) {
+ /* translators: %s: The name of a JSON field expecting a valid UUID. */
+ return new WP_Error( 'rest_invalid_uuid', sprintf( __( '%s is not a valid UUID.' ), $param ) );
+ }
+ break;
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( isset( $args['maxItems'] ) && count( $value ) > $args['maxItems'] ) {
- return new WP_Error(
- 'test_too_many_items',
- sprintf(
- /* translators: 1: Parameter, 2: Number. */
- _n(
- '%1$s must contain at most %2$s item.',
- '%1$s must contain at most %2$s items.',
- $args['maxItems']
- ),
- $param,
- number_format_i18n( $args['maxItems'] )
- )
- );
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ return true;
+}
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( ! empty( $args['uniqueItems'] ) && ! rest_validate_array_contains_unique_items( $value ) ) {
- /* translators: 1: Parameter. */
- return new WP_Error( 'rest_duplicate_items', sprintf( __( '%1$s has duplicate items.' ), $param ) );
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+/**
+ * Validates a null value based on a schema.
+ *
+ * @since 5.7.0
+ *
+ * @param mixed $value The value to validate.
+ * @param string $param The parameter name, used in error messages.
+ * @return true|WP_Error
+ */
+function rest_validate_null_value_from_schema( $value, $param ) {
+ if ( null !== $value ) {
+ return new WP_Error(
+ 'rest_invalid_type',
+ /* translators: 1: Parameter, 2: Type name. */
+ sprintf( __( '%1$s is not of type %2$s.' ), $param, 'null' ),
+ array( 'param' => $param )
+ );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( 'object' === $args['type'] ) {
- if ( ! rest_is_object( $value ) ) {
- return new WP_Error(
- 'rest_invalid_type',
- /* translators: 1: Parameter, 2: Type name. */
- sprintf( __( '%1$s is not of type %2$s.' ), $param, 'object' ),
- array( 'param' => $param )
- );
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ return true;
+}
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $value = rest_sanitize_object( $value );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+/**
+ * Validates a boolean value based on a schema.
+ *
+ * @since 5.7.0
+ *
+ * @param mixed $value The value to validate.
+ * @param string $param The parameter name, used in error messages.
+ * @return true|WP_Error
+ */
+function rest_validate_boolean_value_from_schema( $value, $param ) {
+ if ( ! rest_is_boolean( $value ) ) {
+ return new WP_Error(
+ 'rest_invalid_type',
+ /* translators: 1: Parameter, 2: Type name. */
+ sprintf( __( '%1$s is not of type %2$s.' ), $param, 'boolean' ),
+ array( 'param' => $param )
+ );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( isset( $args['required'] ) && is_array( $args['required'] ) ) { // schema version 4
- foreach ( $args['required'] as $name ) {
- if ( ! array_key_exists( $name, $value ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ return true;
+}
+
+/**
+ * Validates an object value based on a schema.
+ *
+ * @since 5.7.0
+ *
+ * @param mixed $value The value to validate.
+ * @param array $args Schema array to use for validation.
+ * @param string $param The parameter name, used in error messages.
+ * @return true|WP_Error
+ */
+function rest_validate_object_value_from_schema( $value, $args, $param ) {
+ if ( ! rest_is_object( $value ) ) {
+ return new WP_Error(
+ 'rest_invalid_type',
+ /* translators: 1: Parameter, 2: Type name. */
+ sprintf( __( '%1$s is not of type %2$s.' ), $param, 'object' ),
+ array( 'param' => $param )
+ );
+ }
+
+ $value = rest_sanitize_object( $value );
+
+ if ( isset( $args['required'] ) && is_array( $args['required'] ) ) { // schema version 4
+ foreach ( $args['required'] as $name ) {
+ if ( ! array_key_exists( $name, $value ) ) {
+ return new WP_Error(
+ 'rest_property_required',
</ins><span class="cx" style="display: block; padding: 0 10px"> /* translators: 1: Property of an object, 2: Parameter. */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- return new WP_Error( 'rest_property_required', sprintf( __( '%1$s is a required property of %2$s.' ), $name, $param ) );
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ sprintf( __( '%1$s is a required property of %2$s.' ), $name, $param )
+ );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- } elseif ( isset( $args['properties'] ) ) { // schema version 3
- foreach ( $args['properties'] as $name => $property ) {
- if ( isset( $property['required'] ) && true === $property['required'] && ! array_key_exists( $name, $value ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ }
+ } elseif ( isset( $args['properties'] ) ) { // schema version 3
+ foreach ( $args['properties'] as $name => $property ) {
+ if ( isset( $property['required'] ) && true === $property['required'] && ! array_key_exists( $name, $value ) ) {
+ return new WP_Error(
+ 'rest_property_required',
</ins><span class="cx" style="display: block; padding: 0 10px"> /* translators: 1: Property of an object, 2: Parameter. */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- return new WP_Error( 'rest_property_required', sprintf( __( '%1$s is a required property of %2$s.' ), $name, $param ) );
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ sprintf( __( '%1$s is a required property of %2$s.' ), $name, $param )
+ );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- foreach ( $value as $property => $v ) {
- if ( isset( $args['properties'][ $property ] ) ) {
- $is_valid = rest_validate_value_from_schema( $v, $args['properties'][ $property ], $param . '[' . $property . ']' );
- if ( is_wp_error( $is_valid ) ) {
- return $is_valid;
- }
- continue;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ foreach ( $value as $property => $v ) {
+ if ( isset( $args['properties'][ $property ] ) ) {
+ $is_valid = rest_validate_value_from_schema( $v, $args['properties'][ $property ], $param . '[' . $property . ']' );
+ if ( is_wp_error( $is_valid ) ) {
+ return $is_valid;
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ continue;
+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $pattern_property_schema = rest_find_matching_pattern_property_schema( $property, $args );
- if ( null !== $pattern_property_schema ) {
- $is_valid = rest_validate_value_from_schema( $v, $pattern_property_schema, $param . '[' . $property . ']' );
- if ( is_wp_error( $is_valid ) ) {
- return $is_valid;
- }
- continue;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $pattern_property_schema = rest_find_matching_pattern_property_schema( $property, $args );
+ if ( null !== $pattern_property_schema ) {
+ $is_valid = rest_validate_value_from_schema( $v, $pattern_property_schema, $param . '[' . $property . ']' );
+ if ( is_wp_error( $is_valid ) ) {
+ return $is_valid;
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ continue;
+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( isset( $args['additionalProperties'] ) ) {
- if ( false === $args['additionalProperties'] ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( isset( $args['additionalProperties'] ) ) {
+ if ( false === $args['additionalProperties'] ) {
+ return new WP_Error(
+ 'rest_additional_properties_forbidden',
</ins><span class="cx" style="display: block; padding: 0 10px"> /* translators: %s: Property of an object. */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- return new WP_Error( 'rest_additional_properties_forbidden', sprintf( __( '%1$s is not a valid property of Object.' ), $property ) );
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ sprintf( __( '%1$s is not a valid property of Object.' ), $property )
+ );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( is_array( $args['additionalProperties'] ) ) {
- $is_valid = rest_validate_value_from_schema( $v, $args['additionalProperties'], $param . '[' . $property . ']' );
- if ( is_wp_error( $is_valid ) ) {
- return $is_valid;
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( is_array( $args['additionalProperties'] ) ) {
+ $is_valid = rest_validate_value_from_schema( $v, $args['additionalProperties'], $param . '[' . $property . ']' );
+ if ( is_wp_error( $is_valid ) ) {
+ return $is_valid;
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( isset( $args['minProperties'] ) && count( $value ) < $args['minProperties'] ) {
- return new WP_Error(
- 'rest_too_few_properties',
- sprintf(
- /* translators: 1: Parameter, 2: Number. */
- _n(
- '%1$s must contain at least %2$s property.',
- '%1$s must contain at least %2$s properties.',
- $args['minProperties']
- ),
- $param,
- number_format_i18n( $args['minProperties'] )
- )
- );
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( isset( $args['minProperties'] ) && count( $value ) < $args['minProperties'] ) {
+ return new WP_Error(
+ 'rest_too_few_properties',
+ /* translators: 1: Parameter, 2: Number. */
+ sprintf(
+ _n(
+ '%1$s must contain at least %2$s property.',
+ '%1$s must contain at least %2$s properties.',
+ $args['minProperties']
+ ),
+ $param,
+ number_format_i18n( $args['minProperties'] )
+ )
+ );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( isset( $args['maxProperties'] ) && count( $value ) > $args['maxProperties'] ) {
- return new WP_Error(
- 'rest_too_many_properties',
- sprintf(
- /* translators: 1: Parameter, 2: Number. */
- _n(
- '%1$s must contain at most %2$s property.',
- '%1$s must contain at most %2$s properties.',
- $args['maxProperties']
- ),
- $param,
- number_format_i18n( $args['maxProperties'] )
- )
- );
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( isset( $args['maxProperties'] ) && count( $value ) > $args['maxProperties'] ) {
+ return new WP_Error(
+ 'rest_too_many_properties',
+ /* translators: 1: Parameter, 2: Number. */
+ sprintf(
+ _n(
+ '%1$s must contain at most %2$s property.',
+ '%1$s must contain at most %2$s properties.',
+ $args['maxProperties']
+ ),
+ $param,
+ number_format_i18n( $args['maxProperties'] )
+ )
+ );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( 'null' === $args['type'] ) {
- if ( null !== $value ) {
- return new WP_Error(
- 'rest_invalid_type',
- /* translators: 1: Parameter, 2: Type name. */
- sprintf( __( '%1$s is not of type %2$s.' ), $param, 'null' ),
- array( 'param' => $param )
- );
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ return true;
+}
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- return true;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+/**
+ * Validates an array value based on a schema.
+ *
+ * @since 5.7.0
+ *
+ * @param mixed $value The value to validate.
+ * @param array $args Schema array to use for validation.
+ * @param string $param The parameter name, used in error messages.
+ * @return true|WP_Error
+ */
+function rest_validate_array_value_from_schema( $value, $args, $param ) {
+ if ( ! rest_is_array( $value ) ) {
+ return new WP_Error(
+ 'rest_invalid_type',
+ /* translators: 1: Parameter, 2: Type name. */
+ sprintf( __( '%1$s is not of type %2$s.' ), $param, 'array' ),
+ array( 'param' => $param )
+ );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( in_array( $args['type'], array( 'integer', 'number' ), true ) ) {
- if ( ! is_numeric( $value ) ) {
- return new WP_Error(
- 'rest_invalid_type',
- /* translators: 1: Parameter, 2: Type name. */
- sprintf( __( '%1$s is not of type %2$s.' ), $param, $args['type'] ),
- array( 'param' => $param )
- );
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $value = rest_sanitize_array( $value );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( isset( $args['multipleOf'] ) && fmod( $value, $args['multipleOf'] ) !== 0.0 ) {
- /* translators: 1: Parameter, 2: Multiplier. */
- return new WP_Error( 'rest_invalid_multiple', sprintf( __( '%1$s must be a multiple of %2$s.' ), $param, $args['multipleOf'] ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( isset( $args['items'] ) ) {
+ foreach ( $value as $index => $v ) {
+ $is_valid = rest_validate_value_from_schema( $v, $args['items'], $param . '[' . $index . ']' );
+ if ( is_wp_error( $is_valid ) ) {
+ return $is_valid;
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( 'integer' === $args['type'] && ! rest_is_integer( $value ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( isset( $args['minItems'] ) && count( $value ) < $args['minItems'] ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> return new WP_Error(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'rest_invalid_type',
- /* translators: 1: Parameter, 2: Type name. */
- sprintf( __( '%1$s is not of type %2$s.' ), $param, 'integer' ),
- array( 'param' => $param )
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'rest_too_few_items',
+ /* translators: 1: Parameter, 2: Number. */
+ sprintf(
+ _n(
+ '%1$s must contain at least %2$s item.',
+ '%1$s must contain at least %2$s items.',
+ $args['minItems']
+ ),
+ $param,
+ number_format_i18n( $args['minItems'] )
+ )
</ins><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( 'boolean' === $args['type'] && ! rest_is_boolean( $value ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( isset( $args['maxItems'] ) && count( $value ) > $args['maxItems'] ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> return new WP_Error(
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'test_too_many_items',
+ /* translators: 1: Parameter, 2: Number. */
+ sprintf(
+ _n(
+ '%1$s must contain at most %2$s item.',
+ '%1$s must contain at most %2$s items.',
+ $args['maxItems']
+ ),
+ $param,
+ number_format_i18n( $args['maxItems'] )
+ )
+ );
+ }
+
+ if ( ! empty( $args['uniqueItems'] ) && ! rest_validate_array_contains_unique_items( $value ) ) {
+ /* translators: 1: Parameter. */
+ return new WP_Error( 'rest_duplicate_items', sprintf( __( '%1$s has duplicate items.' ), $param ) );
+ }
+
+ return true;
+}
+
+/**
+ * Validates a number value based on a schema.
+ *
+ * @since 5.7.0
+ *
+ * @param mixed $value The value to validate.
+ * @param array $args Schema array to use for validation.
+ * @param string $param The parameter name, used in error messages.
+ * @return true|WP_Error
+ */
+function rest_validate_number_value_from_schema( $value, $args, $param ) {
+ if ( ! is_numeric( $value ) ) {
+ return new WP_Error(
</ins><span class="cx" style="display: block; padding: 0 10px"> 'rest_invalid_type',
</span><span class="cx" style="display: block; padding: 0 10px"> /* translators: 1: Parameter, 2: Type name. */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- sprintf( __( '%1$s is not of type %2$s.' ), $param, 'boolean' ),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ sprintf( __( '%1$s is not of type %2$s.' ), $param, $args['type'] ),
</ins><span class="cx" style="display: block; padding: 0 10px"> array( 'param' => $param )
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( 'string' === $args['type'] ) {
- if ( ! is_string( $value ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( isset( $args['multipleOf'] ) && fmod( $value, $args['multipleOf'] ) !== 0.0 ) {
+ return new WP_Error(
+ 'rest_invalid_multiple',
+ /* translators: 1: Parameter, 2: Multiplier. */
+ sprintf( __( '%1$s must be a multiple of %2$s.' ), $param, $args['multipleOf'] )
+ );
+ }
+
+ if ( isset( $args['minimum'] ) && ! isset( $args['maximum'] ) ) {
+ if ( ! empty( $args['exclusiveMinimum'] ) && $value <= $args['minimum'] ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> return new WP_Error(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'rest_invalid_type',
- /* translators: 1: Parameter, 2: Type name. */
- sprintf( __( '%1$s is not of type %2$s.' ), $param, 'string' ),
- array( 'param' => $param )
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'rest_out_of_bounds',
+ /* translators: 1: Parameter, 2: Minimum number. */
+ sprintf( __( '%1$s must be greater than %2$d' ), $param, $args['minimum'] )
</ins><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( isset( $args['minLength'] ) && mb_strlen( $value ) < $args['minLength'] ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( empty( $args['exclusiveMinimum'] ) && $value < $args['minimum'] ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> return new WP_Error(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'rest_too_short',
- 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'] )
- )
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'rest_out_of_bounds',
+ /* translators: 1: Parameter, 2: Minimum number. */
+ sprintf( __( '%1$s must be greater than or equal to %2$d' ), $param, $args['minimum'] )
</ins><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( isset( $args['maxLength'] ) && mb_strlen( $value ) > $args['maxLength'] ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( isset( $args['maximum'] ) && ! isset( $args['minimum'] ) ) {
+ if ( ! empty( $args['exclusiveMaximum'] ) && $value >= $args['maximum'] ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> return new WP_Error(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'rest_too_long',
- 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'] )
- )
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'rest_out_of_bounds',
+ /* translators: 1: Parameter, 2: Maximum number. */
+ sprintf( __( '%1$s must be less than %2$d' ), $param, $args['maximum'] )
</ins><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( isset( $args['pattern'] ) && ! rest_validate_json_schema_pattern( $args['pattern'], $value ) ) {
- /* translators: 1: Parameter, 2: Pattern. */
- return new WP_Error( 'rest_invalid_pattern', sprintf( __( '%1$s does not match pattern %2$s.' ), $param, $args['pattern'] ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( empty( $args['exclusiveMaximum'] ) && $value > $args['maximum'] ) {
+ return new WP_Error(
+ 'rest_out_of_bounds',
+ /* translators: 1: Parameter, 2: Maximum number. */
+ sprintf( __( '%1$s must be less than or equal to %2$d' ), $param, $args['maximum'] )
+ );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( ! empty( $args['enum'] ) ) {
- $enum_contains_value = rest_validate_enum( $value, $args, $param );
- if ( is_wp_error( $enum_contains_value ) ) {
- return $enum_contains_value;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( isset( $args['minimum'], $args['maximum'] ) ) {
+ if ( ! empty( $args['exclusiveMinimum'] ) && ! empty( $args['exclusiveMaximum'] ) ) {
+ if ( $value >= $args['maximum'] || $value <= $args['minimum'] ) {
+ return new WP_Error(
+ 'rest_out_of_bounds',
+ /* translators: 1: Parameter, 2: Minimum number, 3: Maximum number. */
+ sprintf(
+ __( '%1$s must be between %2$d (exclusive) and %3$d (exclusive)' ),
+ $param,
+ $args['minimum'],
+ $args['maximum']
+ )
+ );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- }
</del><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // The "format" keyword should only be applied to strings. However, for backward compatibility,
- // we allow the "format" keyword if the type keyword was not specified, or was set to an invalid value.
- if ( isset( $args['format'] )
- && ( ! isset( $args['type'] ) || 'string' === $args['type'] || ! in_array( $args['type'], $allowed_types, true ) )
- ) {
- switch ( $args['format'] ) {
- case 'hex-color':
- if ( ! rest_parse_hex_color( $value ) ) {
- return new WP_Error( 'rest_invalid_hex_color', __( 'Invalid hex color.' ) );
- }
- break;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! empty( $args['exclusiveMinimum'] ) && empty( $args['exclusiveMaximum'] ) ) {
+ if ( $value > $args['maximum'] || $value <= $args['minimum'] ) {
+ return new WP_Error(
+ 'rest_out_of_bounds',
+ /* translators: 1: Parameter, 2: Minimum number, 3: Maximum number. */
+ sprintf(
+ __( '%1$s must be between %2$d (exclusive) and %3$d (inclusive)' ),
+ $param,
+ $args['minimum'],
+ $args['maximum']
+ )
+ );
+ }
+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- case 'date-time':
- if ( ! rest_parse_date( $value ) ) {
- return new WP_Error( 'rest_invalid_date', __( 'Invalid date.' ) );
- }
- break;
-
- case 'email':
- if ( ! is_email( $value ) ) {
- return new WP_Error( 'rest_invalid_email', __( 'Invalid email address.' ) );
- }
- break;
- case 'ip':
- if ( ! rest_is_ip_address( $value ) ) {
- /* translators: %s: IP address. */
- return new WP_Error( 'rest_invalid_ip', sprintf( __( '%s is not a valid IP address.' ), $param ) );
- }
- break;
- case 'uuid':
- if ( ! wp_is_uuid( $value ) ) {
- /* translators: %s: The name of a JSON field expecting a valid UUID. */
- return new WP_Error( 'rest_invalid_uuid', sprintf( __( '%s is not a valid UUID.' ), $param ) );
- }
- break;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! empty( $args['exclusiveMaximum'] ) && empty( $args['exclusiveMinimum'] ) ) {
+ if ( $value >= $args['maximum'] || $value < $args['minimum'] ) {
+ return new WP_Error(
+ 'rest_out_of_bounds',
+ /* translators: 1: Parameter, 2: Minimum number, 3: Maximum number. */
+ sprintf(
+ __( '%1$s must be between %2$d (inclusive) and %3$d (exclusive)' ),
+ $param,
+ $args['minimum'],
+ $args['maximum']
+ )
+ );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- }
</del><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( in_array( $args['type'], array( 'number', 'integer' ), true ) && ( isset( $args['minimum'] ) || isset( $args['maximum'] ) ) ) {
- if ( isset( $args['minimum'] ) && ! isset( $args['maximum'] ) ) {
- if ( ! empty( $args['exclusiveMinimum'] ) && $value <= $args['minimum'] ) {
- /* translators: 1: Parameter, 2: Minimum number. */
- return new WP_Error( 'rest_out_of_bounds', sprintf( __( '%1$s must be greater than %2$d' ), $param, $args['minimum'] ) );
- } elseif ( empty( $args['exclusiveMinimum'] ) && $value < $args['minimum'] ) {
- /* translators: 1: Parameter, 2: Minimum number. */
- return new WP_Error( 'rest_out_of_bounds', sprintf( __( '%1$s must be greater than or equal to %2$d' ), $param, $args['minimum'] ) );
- }
- } elseif ( isset( $args['maximum'] ) && ! isset( $args['minimum'] ) ) {
- if ( ! empty( $args['exclusiveMaximum'] ) && $value >= $args['maximum'] ) {
- /* translators: 1: Parameter, 2: Maximum number. */
- return new WP_Error( 'rest_out_of_bounds', sprintf( __( '%1$s must be less than %2$d' ), $param, $args['maximum'] ) );
- } elseif ( empty( $args['exclusiveMaximum'] ) && $value > $args['maximum'] ) {
- /* translators: 1: Parameter, 2: Maximum number. */
- return new WP_Error( 'rest_out_of_bounds', sprintf( __( '%1$s must be less than or equal to %2$d' ), $param, $args['maximum'] ) );
- }
- } elseif ( isset( $args['maximum'] ) && isset( $args['minimum'] ) ) {
- if ( ! empty( $args['exclusiveMinimum'] ) && ! empty( $args['exclusiveMaximum'] ) ) {
- if ( $value >= $args['maximum'] || $value <= $args['minimum'] ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( empty( $args['exclusiveMinimum'] ) && empty( $args['exclusiveMaximum'] ) ) {
+ if ( $value > $args['maximum'] || $value < $args['minimum'] ) {
+ return new WP_Error(
+ 'rest_out_of_bounds',
</ins><span class="cx" style="display: block; padding: 0 10px"> /* translators: 1: Parameter, 2: Minimum number, 3: Maximum number. */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- return new WP_Error( 'rest_out_of_bounds', sprintf( __( '%1$s must be between %2$d (exclusive) and %3$d (exclusive)' ), $param, $args['minimum'], $args['maximum'] ) );
- }
- } elseif ( empty( $args['exclusiveMinimum'] ) && ! empty( $args['exclusiveMaximum'] ) ) {
- if ( $value >= $args['maximum'] || $value < $args['minimum'] ) {
- /* translators: 1: Parameter, 2: Minimum number, 3: Maximum number. */
- return new WP_Error( 'rest_out_of_bounds', sprintf( __( '%1$s must be between %2$d (inclusive) and %3$d (exclusive)' ), $param, $args['minimum'], $args['maximum'] ) );
- }
- } elseif ( ! empty( $args['exclusiveMinimum'] ) && empty( $args['exclusiveMaximum'] ) ) {
- if ( $value > $args['maximum'] || $value <= $args['minimum'] ) {
- /* translators: 1: Parameter, 2: Minimum number, 3: Maximum number. */
- return new WP_Error( 'rest_out_of_bounds', sprintf( __( '%1$s must be between %2$d (exclusive) and %3$d (inclusive)' ), $param, $args['minimum'], $args['maximum'] ) );
- }
- } elseif ( empty( $args['exclusiveMinimum'] ) && empty( $args['exclusiveMaximum'] ) ) {
- if ( $value > $args['maximum'] || $value < $args['minimum'] ) {
- /* translators: 1: Parameter, 2: Minimum number, 3: Maximum number. */
- return new WP_Error( 'rest_out_of_bounds', sprintf( __( '%1$s must be between %2$d (inclusive) and %3$d (inclusive)' ), $param, $args['minimum'], $args['maximum'] ) );
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ sprintf(
+ __( '%1$s must be between %2$d (inclusive) and %3$d (inclusive)' ),
+ $param,
+ $args['minimum'],
+ $args['maximum']
+ )
+ );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2385,6 +2493,97 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Validates a string value based on a schema.
+ *
+ * @since 5.7.0
+ *
+ * @param mixed $value The value to validate.
+ * @param array $args Schema array to use for validation.
+ * @param string $param The parameter name, used in error messages.
+ * @return true|WP_Error
+ */
+function rest_validate_string_value_from_schema( $value, $args, $param ) {
+ if ( ! is_string( $value ) ) {
+ return new WP_Error(
+ 'rest_invalid_type',
+ /* translators: 1: Parameter, 2: Type name. */
+ sprintf( __( '%1$s is not of type %2$s.' ), $param, 'string' ),
+ array( 'param' => $param )
+ );
+ }
+
+ if ( isset( $args['minLength'] ) && mb_strlen( $value ) < $args['minLength'] ) {
+ return new WP_Error(
+ 'rest_too_short',
+ /* translators: 1: Parameter, 2: Number of characters. */
+ sprintf(
+ _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_too_long',
+ /* translators: 1: Parameter, 2: Number of characters. */
+ sprintf(
+ _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['pattern'] ) && ! rest_validate_json_schema_pattern( $args['pattern'], $value ) ) {
+ return new WP_Error(
+ 'rest_invalid_pattern',
+ /* translators: 1: Parameter, 2: Pattern. */
+ sprintf( __( '%1$s does not match pattern %2$s.' ), $param, $args['pattern'] )
+ );
+ }
+
+ return true;
+}
+
+/**
+ * Validates an integer value based on a schema.
+ *
+ * @since 5.7.0
+ *
+ * @param mixed $value The value to validate.
+ * @param array $args Schema array to use for validation.
+ * @param string $param The parameter name, used in error messages.
+ * @return true|WP_Error
+ */
+function rest_validate_integer_value_from_schema( $value, $args, $param ) {
+ $is_valid_number = rest_validate_number_value_from_schema( $value, $args, $param );
+ if ( is_wp_error( $is_valid_number ) ) {
+ return $is_valid_number;
+ }
+
+ if ( ! rest_is_integer( $value ) ) {
+ return new WP_Error(
+ 'rest_invalid_type',
+ /* translators: 1: Parameter, 2: Type name. */
+ sprintf( __( '%1$s is not of type %2$s.' ), $param, 'integer' ),
+ array( 'param' => $param )
+ );
+ }
+
+ return true;
+}
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Sanitize a value based on a schema.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 4.7.0
</span></span></pre>
</div>
</div>
</body>
</html>