From 01ec77148d88b45461652b30ead43297cd2172ce Mon Sep 17 00:00:00 2001 From: Max Loeb Date: Tue, 25 Sep 2018 21:10:17 -0700 Subject: [PATCH 1/2] return original value when no cast --- src/JsonSchema/Constraints/TypeConstraint.php | 2 + tests/Constraints/CoerciveTest.php | 79 ++++++++++--------- 2 files changed, 42 insertions(+), 39 deletions(-) diff --git a/src/JsonSchema/Constraints/TypeConstraint.php b/src/JsonSchema/Constraints/TypeConstraint.php index 5bfe08a9..3c2ef391 100644 --- a/src/JsonSchema/Constraints/TypeConstraint.php +++ b/src/JsonSchema/Constraints/TypeConstraint.php @@ -337,6 +337,8 @@ protected function toString($value) if ($this->getTypeCheck()->isArray($value) && count($value) === 1) { return $this->toString(reset($value)); } + + return $value; } /** diff --git a/tests/Constraints/CoerciveTest.php b/tests/Constraints/CoerciveTest.php index 587f1f99..53e02e27 100644 --- a/tests/Constraints/CoerciveTest.php +++ b/tests/Constraints/CoerciveTest.php @@ -39,55 +39,49 @@ public function dataCoerceCases() array('array', '[45]', '45', true), // #5 array('object', '{"a":"b"}', null, false), // #6 array('array', '[{"a":"b"}]', null, false), // #7 + array('array', '[1,2]', array(1,2), false), // #8 ), 'integer' => array( - array('string', '"45"', 45, true), // #8 - array('integer', '45', 45, true), // #9 - array('boolean', 'true', 1, true), // #10 - array('boolean', 'false', 0, true), // #11 - array('NULL', 'null', 0, true), // #12 - array('array', '["-45"]', -45, true), // #13 - array('object', '{"a":"b"}', null, false), // #14 - array('array', '["ABC"]', null, false), // #15 + array('string', '"45"', 45, true), // #9 + array('integer', '45', 45, true), // #10 + array('boolean', 'true', 1, true), // #11 + array('boolean', 'false', 0, true), // #12 + array('NULL', 'null', 0, true), // #13 + array('array', '["-45"]', -45, true), // #14 + array('object', '{"a":"b"}', null, false), // #15 + array('array', '["ABC"]', null, false), // #16 ), 'boolean' => array( - array('string', '"true"', true, true), // #16 - array('integer', '1', true, true), // #17 - array('boolean', 'true', true, true), // #18 - array('NULL', 'null', false, true), // #19 - array('array', '["true"]', true, true), // #20 - array('object', '{"a":"b"}', null, false), // #21 - array('string', '""', null, false), // #22 - array('string', '"ABC"', null, false), // #23 - array('integer', '2', null, false), // #24 + array('string', '"true"', true, true), // #17 + array('integer', '1', true, true), // #18 + array('boolean', 'true', true, true), // #19 + array('NULL', 'null', false, true), // #20 + array('array', '["true"]', true, true), // #21 + array('object', '{"a":"b"}', null, false), // #22 + array('string', '""', null, false), // #23 + array('string', '"ABC"', null, false), // #24 + array('integer', '2', null, false), // #25 ), 'NULL' => array( - array('string', '""', null, true), // #25 - array('integer', '0', null, true), // #26 - array('boolean', 'false', null, true), // #27 - array('NULL', 'null', null, true), // #28 - array('array', '[0]', null, true), // #29 - array('object', '{"a":"b"}', null, false), // #30 - array('string', '"null"', null, false), // #31 - array('integer', '-1', null, false), // #32 + array('string', '""', null, true), // #26 + array('integer', '0', null, true), // #27 + array('boolean', 'false', null, true), // #28 + array('NULL', 'null', null, true), // #29 + array('array', '[0]', null, true), // #30 + array('object', '{"a":"b"}', null, false), // #31 + array('string', '"null"', null, false), // #32 + array('integer', '-1', null, false), // #33 ), 'array' => array( - array('string', '"ABC"', array('ABC'), true), // #33 - array('integer', '45', array(45), true), // #34 - array('boolean', 'true', array(true), true), // #35 - array('NULL', 'null', array(null), true), // #36 - array('array', '["ABC"]', array('ABC'), true), // #37 - array('object', '{"a":"b"}', null, false), // #38 + array('string', '"ABC"', array('ABC'), true), // #34 + array('integer', '45', array(45), true), // #35 + array('boolean', 'true', array(true), true), // #36 + array('NULL', 'null', array(null), true), // #37 + array('array', '["ABC"]', array('ABC'), true), // #38 + array('object', '{"a":"b"}', null, false), // #39 ), ); - // #39 check post-coercion validation (to array) - $tests[] = array( - '{"properties":{"propertyOne":{"type":"array","items":[{"type":"number"}]}}}', - '{"propertyOne":"ABC"}', - 'string', null, null, false - ); - // #40 check multiple types (first valid) $tests[] = array( '{"properties":{"propertyOne":{"type":["number", "string"]}}}', @@ -172,7 +166,14 @@ public function dataCoerceCases() 'string', 'boolean', false, true ); - foreach ($types as $toType => $testCases) { + // #52 check post-coercion validation (to array) + $tests[] = array( + '{"properties":{"propertyOne":{"type":"array","items":[{"type":"number"}]}}}', + '{"propertyOne":"ABC"}', + 'string', null, null, false + ); + + foreach ($types as $toType => $testCases) { foreach ($testCases as $testCase) { $tests[] = array( sprintf('{"properties":{"propertyOne":{"type":"%s"}}}', strtolower($toType)), From a754bd35e34936ee4e5666ffbc86c37c237e3057 Mon Sep 17 00:00:00 2001 From: Max Loeb Date: Wed, 3 Oct 2018 02:38:47 -0700 Subject: [PATCH 2/2] linting --- tests/Constraints/CoerciveTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Constraints/CoerciveTest.php b/tests/Constraints/CoerciveTest.php index 53e02e27..22684b3e 100644 --- a/tests/Constraints/CoerciveTest.php +++ b/tests/Constraints/CoerciveTest.php @@ -39,7 +39,7 @@ public function dataCoerceCases() array('array', '[45]', '45', true), // #5 array('object', '{"a":"b"}', null, false), // #6 array('array', '[{"a":"b"}]', null, false), // #7 - array('array', '[1,2]', array(1,2), false), // #8 + array('array', '[1,2]', array(1, 2), false), // #8 ), 'integer' => array( array('string', '"45"', 45, true), // #9 @@ -173,7 +173,7 @@ public function dataCoerceCases() 'string', null, null, false ); - foreach ($types as $toType => $testCases) { + foreach ($types as $toType => $testCases) { foreach ($testCases as $testCase) { $tests[] = array( sprintf('{"properties":{"propertyOne":{"type":"%s"}}}', strtolower($toType)),