<!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>[48357] trunk: REST API: Add support for the uniqueItems keyword.</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/48357">48357</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/48357","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>2020-07-07 03:20:34 +0000 (Tue, 07 Jul 2020)</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: Add support for the uniqueItems keyword.

Props sorenbronsted.
Fixes <a href="https://core.trac.wordpress.org/ticket/48821">#48821</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesrestapiphp">trunk/src/wp-includes/rest-api.php</a></li>
<li><a href="#trunktestsphpunittestsrestapirestschemasanitizationphp">trunk/tests/phpunit/tests/rest-api/rest-schema-sanitization.php</a></li>
<li><a href="#trunktestsphpunittestsrestapirestschemavalidationphp">trunk/tests/phpunit/tests/rest-api/rest-schema-validation.php</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li>trunk/tests/phpunit/tests/rest-api/json_schema_test_suite/</li>
<li><a href="#trunktestsphpunittestsrestapijson_schema_test_suiteuniqueitemsjson">trunk/tests/phpunit/tests/rest-api/json_schema_test_suite/uniqueitems.json</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        2020-07-07 00:53:41 UTC (rev 48356)
+++ trunk/src/wp-includes/rest-api.php  2020-07-07 03:20:34 UTC (rev 48357)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1439,6 +1439,63 @@
</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">+ * Checks if an array is made up of unique items.
+ *
+ * @since 5.5.0
+ *
+ * @param array $array The array to check.
+ * @return bool True if the array contains unique items, false otherwise.
+ */
+function rest_validate_array_contains_unique_items( $array ) {
+       $seen = array();
+
+       foreach ( $array as $item ) {
+               $stabilized = rest_stabilize_value( $item );
+               $key        = serialize( $stabilized );
+
+               if ( ! isset( $seen[ $key ] ) ) {
+                       $seen[ $key ] = true;
+
+                       continue;
+               }
+
+               return false;
+       }
+
+       return true;
+}
+
+/**
+ * Stabilizes a value following JSON Schema semantics.
+ *
+ * For lists, order is preserved. For objects, properties are reordered alphabetically.
+ *
+ * @since 5.5.0
+ *
+ * @param mixed $value The value to stabilize. Must already be sanitized. Objects should have been converted to arrays.
+ * @return mixed The stabilized value.
+ */
+function rest_stabilize_value( $value ) {
+       if ( is_scalar( $value ) || is_null( $value ) ) {
+               return $value;
+       }
+
+       if ( is_object( $value ) ) {
+               _doing_it_wrong( __FUNCTION__, __( 'Cannot stabilize objects. Convert the object to an array first.' ), '5.5.0' );
+
+               return $value;
+       }
+
+       ksort( $value );
+
+       foreach ( $value as $k => $v ) {
+               $value[ $k ] = rest_stabilize_value( $v );
+       }
+
+       return $value;
+}
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px">  * Validate 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 class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1448,8 +1505,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 5.4.0 Convert an empty string to an empty object.
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 5.5.0 Add the "uuid" and "hex-color" formats.
</span><span class="cx" style="display: block; padding: 0 10px">  *              Support the "minLength", "maxLength" and "pattern" keywords for strings.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ *              Support the "minItems", "maxItems" and "uniqueItems" keywords for arrays.
</ins><span class="cx" style="display: block; padding: 0 10px">  *              Validate required properties.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- *              Support the "minItems" and "maxItems" keywords for arrays.
</del><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @param mixed  $value The value to validate.
</span><span class="cx" style="display: block; padding: 0 10px">  * @param array  $args  Schema array to use for validation.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1492,10 +1549,12 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $value = rest_sanitize_array( $value );
</span><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 $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">+         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><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1508,6 +1567,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        /* translators: 1: Parameter, 2: Number. */
</span><span class="cx" style="display: block; padding: 0 10px">                        return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s must contain at most %2$s items.' ), $param, number_format_i18n( $args['maxItems'] ) ) );
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><ins style="background-color: #dfd; 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_invalid_param', sprintf( __( '%1$s has duplicate items.' ), $param ) );
+               }
</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">        if ( 'object' === $args['type'] ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1718,7 +1782,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">  * @param mixed  $value The value to sanitize.
</span><span class="cx" style="display: block; padding: 0 10px">  * @param array  $args  Schema array to use for sanitization.
</span><span class="cx" style="display: block; padding: 0 10px">  * @param string $param The parameter name, used in error messages.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @return mixed The sanitized value.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @return mixed|WP_Error The sanitized value or a WP_Error instance if the value cannot be safely sanitized.
</ins><span class="cx" style="display: block; padding: 0 10px">  */
</span><span class="cx" style="display: block; padding: 0 10px"> function rest_sanitize_value_from_schema( $value, $args, $param = '' ) {
</span><span class="cx" style="display: block; padding: 0 10px">        $allowed_types = array( 'array', 'object', 'string', 'number', 'integer', 'boolean', 'null' );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1750,12 +1814,15 @@
</span><span class="cx" style="display: block; padding: 0 10px">        if ( 'array' === $args['type'] ) {
</span><span class="cx" style="display: block; padding: 0 10px">                $value = rest_sanitize_array( $value );
</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['items'] ) ) {
-                       return $value;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( ! empty( $args['items'] ) ) {
+                       foreach ( $value as $index => $v ) {
+                               $value[ $index ] = rest_sanitize_value_from_schema( $v, $args['items'], $param . '[' . $index . ']' );
+                       }
</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">-                foreach ( $value as $index => $v ) {
-                       $value[ $index ] = rest_sanitize_value_from_schema( $v, $args['items'], $param . '[' . $index . ']' );
</del><ins style="background-color: #dfd; 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_invalid_param', sprintf( __( '%1$s has duplicate items.' ), $param ) );
</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">                return $value;
</span></span></pre></div>
<a id="trunktestsphpunittestsrestapijson_schema_test_suiteuniqueitemsjson"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/rest-api/json_schema_test_suite/uniqueitems.json</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/rest-api/json_schema_test_suite/uniqueitems.json                                (rev 0)
+++ trunk/tests/phpunit/tests/rest-api/json_schema_test_suite/uniqueitems.json  2020-07-07 03:20:34 UTC (rev 48357)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,374 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+[
+  {
+       "description": "uniqueItems validation",
+       "schema": {"uniqueItems": true},
+       "tests": [
+         {
+               "description": "unique array of integers is valid",
+               "data": [1, 2],
+               "valid": true
+         },
+         {
+               "description": "non-unique array of integers is invalid",
+               "data": [1, 1],
+               "valid": false
+         },
+         {
+               "description": "numbers are unique if mathematically unequal",
+               "data": [1.0, 1.00, 1],
+               "valid": false
+         },
+         {
+               "description": "false is not equal to zero",
+               "data": [0, false],
+               "valid": true
+         },
+         {
+               "description": "true is not equal to one",
+               "data": [1, true],
+               "valid": true
+         },
+         {
+               "description": "unique array of objects is valid",
+               "data": [{"foo": "bar"}, {"foo": "baz"}],
+               "valid": true
+         },
+         {
+               "description": "non-unique array of objects is invalid",
+               "data": [{"foo": "bar"}, {"foo": "bar"}],
+               "valid": false
+         },
+         {
+               "description": "unique array of nested objects is valid",
+               "data": [
+                 {"foo": {"bar" : {"baz" : true}}},
+                 {"foo": {"bar" : {"baz" : false}}}
+               ],
+               "valid": true
+         },
+         {
+               "description": "non-unique array of nested objects is invalid",
+               "data": [
+                 {"foo": {"bar" : {"baz" : true}}},
+                 {"foo": {"bar" : {"baz" : true}}}
+               ],
+               "valid": false
+         },
+         {
+               "description": "unique array of arrays is valid",
+               "data": [["foo"], ["bar"]],
+               "valid": true
+         },
+         {
+               "description": "non-unique array of arrays is invalid",
+               "data": [["foo"], ["foo"]],
+               "valid": false
+         },
+         {
+               "description": "1 and true are unique",
+               "data": [1, true],
+               "valid": true
+         },
+         {
+               "description": "0 and false are unique",
+               "data": [0, false],
+               "valid": true
+         },
+         {
+               "description": "[1] and [true] are unique",
+               "data": [[1], [true]],
+               "valid": true
+         },
+         {
+               "description": "[0] and [false] are unique",
+               "data": [[0], [false]],
+               "valid": true
+         },
+         {
+               "description": "nested [1] and [true] are unique",
+               "data": [[[1], "foo"], [[true], "foo"]],
+               "valid": true
+         },
+         {
+               "description": "nested [0] and [false] are unique",
+               "data": [[[0], "foo"], [[false], "foo"]],
+               "valid": true
+         },
+         {
+               "description": "unique heterogeneous types are valid",
+               "data": [{}, [1], true, null, 1, "{}"],
+               "valid": true
+         },
+         {
+               "description": "non-unique heterogeneous types are invalid",
+               "data": [{}, [1], true, null, {}, 1],
+               "valid": false
+         },
+         {
+               "description": "different objects are unique",
+               "data": [{"a": 1, "b": 2}, {"a": 2, "b": 1}],
+               "valid": true
+         },
+         {
+               "description": "objects are non-unique despite key order",
+               "data": [{"a": 1, "b": 2}, {"b": 2, "a": 1}],
+               "valid": false
+         }
+       ]
+  },
+  {
+       "description": "uniqueItems with an array of items",
+       "schema": {
+         "items": [{"type": "boolean"}, {"type": "boolean"}],
+         "uniqueItems": true
+       },
+       "tests": [
+         {
+               "description": "[false, true] from items array is valid",
+               "data": [false, true],
+               "valid": true
+         },
+         {
+               "description": "[true, false] from items array is valid",
+               "data": [true, false],
+               "valid": true
+         },
+         {
+               "description": "[false, false] from items array is not valid",
+               "data": [false, false],
+               "valid": false
+         },
+         {
+               "description": "[true, true] from items array is not valid",
+               "data": [true, true],
+               "valid": false
+         },
+         {
+               "description": "unique array extended from [false, true] is valid",
+               "data": [false, true, "foo", "bar"],
+               "valid": true
+         },
+         {
+               "description": "unique array extended from [true, false] is valid",
+               "data": [true, false, "foo", "bar"],
+               "valid": true
+         },
+         {
+               "description": "non-unique array extended from [false, true] is not valid",
+               "data": [false, true, "foo", "foo"],
+               "valid": false
+         },
+         {
+               "description": "non-unique array extended from [true, false] is not valid",
+               "data": [true, false, "foo", "foo"],
+               "valid": false
+         }
+       ]
+  },
+  {
+       "description": "uniqueItems with an array of items and additionalItems=false",
+       "schema": {
+         "items": [{"type": "boolean"}, {"type": "boolean"}],
+         "uniqueItems": true,
+         "additionalItems": false
+       },
+       "tests": [
+         {
+               "description": "[false, true] from items array is valid",
+               "data": [false, true],
+               "valid": true
+         },
+         {
+               "description": "[true, false] from items array is valid",
+               "data": [true, false],
+               "valid": true
+         },
+         {
+               "description": "[false, false] from items array is not valid",
+               "data": [false, false],
+               "valid": false
+         },
+         {
+               "description": "[true, true] from items array is not valid",
+               "data": [true, true],
+               "valid": false
+         },
+         {
+               "description": "extra items are invalid even if unique",
+               "data": [false, true, null],
+               "valid": false
+         }
+       ]
+  },
+  {
+       "description": "uniqueItems=false validation",
+       "schema": { "uniqueItems": false },
+       "tests": [
+         {
+               "description": "unique array of integers is valid",
+               "data": [1, 2],
+               "valid": true
+         },
+         {
+               "description": "non-unique array of integers is valid",
+               "data": [1, 1],
+               "valid": true
+         },
+         {
+               "description": "numbers are unique if mathematically unequal",
+               "data": [1.0, 1.00, 1],
+               "valid": true
+         },
+         {
+               "description": "false is not equal to zero",
+               "data": [0, false],
+               "valid": true
+         },
+         {
+               "description": "true is not equal to one",
+               "data": [1, true],
+               "valid": true
+         },
+         {
+               "description": "unique array of objects is valid",
+               "data": [{"foo": "bar"}, {"foo": "baz"}],
+               "valid": true
+         },
+         {
+               "description": "non-unique array of objects is valid",
+               "data": [{"foo": "bar"}, {"foo": "bar"}],
+               "valid": true
+         },
+         {
+               "description": "unique array of nested objects is valid",
+               "data": [
+                 {"foo": {"bar" : {"baz" : true}}},
+                 {"foo": {"bar" : {"baz" : false}}}
+               ],
+               "valid": true
+         },
+         {
+               "description": "non-unique array of nested objects is valid",
+               "data": [
+                 {"foo": {"bar" : {"baz" : true}}},
+                 {"foo": {"bar" : {"baz" : true}}}
+               ],
+               "valid": true
+         },
+         {
+               "description": "unique array of arrays is valid",
+               "data": [["foo"], ["bar"]],
+               "valid": true
+         },
+         {
+               "description": "non-unique array of arrays is valid",
+               "data": [["foo"], ["foo"]],
+               "valid": true
+         },
+         {
+               "description": "1 and true are unique",
+               "data": [1, true],
+               "valid": true
+         },
+         {
+               "description": "0 and false are unique",
+               "data": [0, false],
+               "valid": true
+         },
+         {
+               "description": "unique heterogeneous types are valid",
+               "data": [{}, [1], true, null, 1],
+               "valid": true
+         },
+         {
+               "description": "non-unique heterogeneous types are valid",
+               "data": [{}, [1], true, null, {}, 1],
+               "valid": true
+         }
+       ]
+  },
+  {
+       "description": "uniqueItems=false with an array of items",
+       "schema": {
+         "items": [{"type": "boolean"}, {"type": "boolean"}],
+         "uniqueItems": false
+       },
+       "tests": [
+         {
+               "description": "[false, true] from items array is valid",
+               "data": [false, true],
+               "valid": true
+         },
+         {
+               "description": "[true, false] from items array is valid",
+               "data": [true, false],
+               "valid": true
+         },
+         {
+               "description": "[false, false] from items array is valid",
+               "data": [false, false],
+               "valid": true
+         },
+         {
+               "description": "[true, true] from items array is valid",
+               "data": [true, true],
+               "valid": true
+         },
+         {
+               "description": "unique array extended from [false, true] is valid",
+               "data": [false, true, "foo", "bar"],
+               "valid": true
+         },
+         {
+               "description": "unique array extended from [true, false] is valid",
+               "data": [true, false, "foo", "bar"],
+               "valid": true
+         },
+         {
+               "description": "non-unique array extended from [false, true] is valid",
+               "data": [false, true, "foo", "foo"],
+               "valid": true
+         },
+         {
+               "description": "non-unique array extended from [true, false] is valid",
+               "data": [true, false, "foo", "foo"],
+               "valid": true
+         }
+       ]
+  },
+  {
+       "description": "uniqueItems=false with an array of items and additionalItems=false",
+       "schema": {
+         "items": [{"type": "boolean"}, {"type": "boolean"}],
+         "uniqueItems": false,
+         "additionalItems": false
+       },
+       "tests": [
+         {
+               "description": "[false, true] from items array is valid",
+               "data": [false, true],
+               "valid": true
+         },
+         {
+               "description": "[true, false] from items array is valid",
+               "data": [true, false],
+               "valid": true
+         },
+         {
+               "description": "[false, false] from items array is valid",
+               "data": [false, false],
+               "valid": true
+         },
+         {
+               "description": "[true, true] from items array is valid",
+               "data": [true, true],
+               "valid": true
+         },
+         {
+               "description": "extra items are invalid even if unique",
+               "data": [false, true, null],
+               "valid": false
+         }
+       ]
+  }
+]
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/tests/phpunit/tests/rest-api/json_schema_test_suite/uniqueitems.json
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="trunktestsphpunittestsrestapirestschemasanitizationphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/rest-api/rest-schema-sanitization.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/rest-api/rest-schema-sanitization.php   2020-07-07 00:53:41 UTC (rev 48356)
+++ trunk/tests/phpunit/tests/rest-api/rest-schema-sanitization.php     2020-07-07 03:20:34 UTC (rev 48357)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -464,4 +464,26 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertNull( rest_sanitize_value_from_schema( array( 'Hello!' ), $schema ) );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       /**
+        * @ticket 48821
+        */
+       public function test_unique_items_after_sanitization() {
+               $schema = array(
+                       'type'        => 'array',
+                       'uniqueItems' => true,
+                       'items'       => array(
+                               'type'   => 'string',
+                               'format' => 'uri',
+                       ),
+               );
+
+               $data = array(
+                       'https://example.org/hello%20world',
+                       'https://example.org/hello world',
+               );
+
+               $this->assertTrue( rest_validate_value_from_schema( $data, $schema ) );
+               $this->assertWPError( rest_sanitize_value_from_schema( $data, $schema ) );
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunktestsphpunittestsrestapirestschemavalidationphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/rest-api/rest-schema-validation.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/rest-api/rest-schema-validation.php     2020-07-07 00:53:41 UTC (rev 48356)
+++ trunk/tests/phpunit/tests/rest-api/rest-schema-validation.php       2020-07-07 03:20:34 UTC (rev 48357)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -507,7 +507,7 @@
</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">-         * @ticket 48818
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @ticket       48818
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><span class="cx" style="display: block; padding: 0 10px">         * @dataProvider data_required_property
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -535,7 +535,7 @@
</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">-         * @ticket 48818
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @ticket       48818
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><span class="cx" style="display: block; padding: 0 10px">         * @dataProvider data_required_property
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -577,7 +577,7 @@
</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">-         * @ticket 48818
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @ticket       48818
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><span class="cx" style="display: block; padding: 0 10px">         * @dataProvider data_required_nested_property
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -610,7 +610,7 @@
</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">-         * @ticket 48818
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @ticket       48818
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><span class="cx" style="display: block; padding: 0 10px">         * @dataProvider data_required_nested_property
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -669,7 +669,7 @@
</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">-         * @ticket 48818
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @ticket       48818
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><span class="cx" style="display: block; padding: 0 10px">         * @dataProvider data_required_deeply_nested_property
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -709,7 +709,7 @@
</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">-         * @ticket 48818
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @ticket       48818
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><span class="cx" style="display: block; padding: 0 10px">         * @dataProvider data_required_deeply_nested_property
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -749,7 +749,7 @@
</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">-         * @ticket 48818
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @ticket       48818
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><span class="cx" style="display: block; padding: 0 10px">         * @dataProvider data_required_deeply_nested_property
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -905,4 +905,139 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertWPError( rest_validate_value_from_schema( array( 1, 2, 3 ), $schema ) );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertWPError( rest_validate_value_from_schema( 'foobar', $schema ) );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       /**
+        * @ticket       48821
+        *
+        * @dataProvider data_unique_items
+        */
+       public function test_unique_items( $test, $suite ) {
+               $test_description = $suite['description'] . ': ' . $test['description'];
+               $message          = $test_description . ': ' . var_export( $test['data'], true );
+
+               $valid = rest_validate_value_from_schema( $test['data'], $suite['schema'] );
+
+               if ( $test['valid'] ) {
+                       $this->assertTrue( $valid, $message );
+               } else {
+                       $this->assertWPError( $valid, $message );
+               }
+       }
+
+       public function data_unique_items() {
+               $all_types = array( 'object', 'array', 'null', 'number', 'integer', 'boolean', 'string' );
+
+               // the following test suites is not supported at the moment
+               $skip   = array(
+                       'uniqueItems with an array of items',
+                       'uniqueItems with an array of items and additionalItems=false',
+                       'uniqueItems=false with an array of items',
+                       'uniqueItems=false with an array of items and additionalItems=false',
+               );
+               $suites = json_decode( file_get_contents( __DIR__ . '/json_schema_test_suite/uniqueitems.json' ), true );
+
+               $tests = array();
+
+               foreach ( $suites as $suite ) {
+                       if ( in_array( $suite['description'], $skip, true ) ) {
+                               continue;
+                       }
+                       // type is required for our implementation
+                       if ( ! isset( $suite['schema']['type'] ) ) {
+                               $suite['schema']['type'] = 'array';
+                       }
+                       // items is required for our implementation
+                       if ( ! isset( $suite['schema']['items'] ) ) {
+                               $suite['schema']['items'] = array(
+                                       'type'  => $all_types,
+                                       'items' => array(
+                                               'type' => $all_types,
+                                       ),
+                               );
+                       }
+                       foreach ( $suite['tests'] as $test ) {
+                               $tests[] = array( $test, $suite );
+                       }
+               }
+
+               return $tests;
+       }
+
+       /**
+        * @ticket 48821
+        */
+       public function test_unique_items_deep_objects() {
+               $schema = array(
+                       'type'        => 'array',
+                       'uniqueItems' => true,
+                       'items'       => array(
+                               'type'       => 'object',
+                               'properties' => array(
+                                       'release' => array(
+                                               'type'       => 'object',
+                                               'properties' => array(
+                                                       'name'    => array(
+                                                               'type' => 'string',
+                                                       ),
+                                                       'version' => array(
+                                                               'type' => 'string',
+                                                       ),
+                                               ),
+                                       ),
+                               ),
+                       ),
+               );
+
+               $data = array(
+                       array(
+                               'release' => array(
+                                       'name'    => 'Kirk',
+                                       'version' => '5.3',
+                               ),
+                       ),
+                       array(
+                               'release' => array(
+                                       'version' => '5.3',
+                                       'name'    => 'Kirk',
+                               ),
+                       ),
+               );
+
+               $this->assertWPError( rest_validate_value_from_schema( $data, $schema ) );
+
+               $data[0]['release']['version'] = '5.3.0';
+               $this->assertTrue( rest_validate_value_from_schema( $data, $schema ) );
+       }
+
+       /**
+        * @ticket 48821
+        */
+       public function test_unique_items_deep_arrays() {
+               $schema = array(
+                       'type'        => 'array',
+                       'uniqueItems' => true,
+                       'items'       => array(
+                               'type'  => 'array',
+                               'items' => array(
+                                       'type' => 'string',
+                               ),
+                       ),
+               );
+
+               $data = array(
+                       array(
+                               'Kirk',
+                               'Jaco',
+                       ),
+                       array(
+                               'Kirk',
+                               'Jaco',
+                       ),
+               );
+
+               $this->assertWPError( rest_validate_value_from_schema( $data, $schema ) );
+
+               $data[1] = array_reverse( $data[1] );
+               $this->assertTrue( rest_validate_value_from_schema( $data, $schema ) );
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>

</body>
</html>