From c230cad5f25ff27dc06377840082cacc05463de8 Mon Sep 17 00:00:00 2001 From: Michael Chiocca Date: Tue, 7 May 2013 07:10:04 -0700 Subject: [PATCH] Fix issues with uniqueItems in arrays. Use var_export() instead of print_r(). --- src/JsonSchema/Constraints/Collection.php | 2 +- .../Tests/Constraints/UniqueItemsTest.php | 56 +++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/src/JsonSchema/Constraints/Collection.php b/src/JsonSchema/Constraints/Collection.php index 3565aaf..4ca8e1b 100644 --- a/src/JsonSchema/Constraints/Collection.php +++ b/src/JsonSchema/Constraints/Collection.php @@ -36,7 +36,7 @@ class Collection extends Constraint if (isset($schema->uniqueItems)) { $unique = $value; if (is_array($value) && count($value)) { - $unique = array_map(function($e) { return print_r($e, true); }, $value); + $unique = array_map(function($e) { return var_export($e, true); }, $value); } if (count(array_unique($unique)) != count($value)) { $this->addError($path, "There are no duplicates allowed in the array"); diff --git a/tests/JsonSchema/Tests/Constraints/UniqueItemsTest.php b/tests/JsonSchema/Tests/Constraints/UniqueItemsTest.php index 1772e9e..3006bb4 100644 --- a/tests/JsonSchema/Tests/Constraints/UniqueItemsTest.php +++ b/tests/JsonSchema/Tests/Constraints/UniqueItemsTest.php @@ -34,6 +34,27 @@ class UniqueItemsTest extends BaseTestCase "type": "array", "uniqueItems": true }' + ), + array( + '[1.0, 1.00, 1]', + '{ + "type": "array", + "uniqueItems": true + }' + ), + array( + '[["foo"], ["foo"]]', + '{ + "type": "array", + "uniqueItems": true + }' + ), + array( + '[{}, [1], true, null, {}, 1]', + '{ + "type": "array", + "uniqueItems": true + }' ) ); } @@ -54,6 +75,41 @@ class UniqueItemsTest extends BaseTestCase "type": "array", "uniqueItems": true }' + ), + array( + '[1, true]', + '{ + "type": "array", + "uniqueItems": true + }' + ), + array( + '[0, false]', + '{ + "type": "array", + "uniqueItems": true + }' + ), + array( + '[{"foo": {"bar" : {"baz" : true}}}, {"foo": {"bar" : {"baz" : false}}}]', + '{ + "type": "array", + "uniqueItems": true + }' + ), + array( + '[["foo"], ["bar"]]', + '{ + "type": "array", + "uniqueItems": true + }' + ), + array( + '[{}, [1], true, null, 1]', + '{ + "type": "array", + "uniqueItems": true + }' ) ); }