diff --git a/CHANGELOG.md b/CHANGELOG.md index b21b6d8c..e03a6255 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,10 +10,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Run PHPStan using the lowest and highest php version ([#811](https://github.com/jsonrainbow/json-schema/pull/811)) ### Fixed - Use parallel-lint and cs2pr for improved feedback on linting errors ([#812](https://github.com/jsonrainbow/json-schema/pull/812)) +- Array with number values with mathematical equality are considered valid ([#813](https://github.com/jsonrainbow/json-schema/pull/813)) ## Changed - Correct PHPStan findings in validator ([#808](https://github.com/jsonrainbow/json-schema/pull/808)) - ## [6.3.1] - 2025-03-18 ### Fixed - ensure numeric issues in const are correctly evaluated ([#805](https://github.com/jsonrainbow/json-schema/pull/805)) diff --git a/src/JsonSchema/Tool/DeepComparer.php b/src/JsonSchema/Tool/DeepComparer.php index fb99b141..f743d353 100644 --- a/src/JsonSchema/Tool/DeepComparer.php +++ b/src/JsonSchema/Tool/DeepComparer.php @@ -17,9 +17,18 @@ public static function isEqual($left, $right): bool } $isLeftScalar = is_scalar($left); + $isLeftNumber = is_int($left) || is_float($left); $isRightScalar = is_scalar($right); + $isRightNumber = is_int($right) || is_float($right); if ($isLeftScalar && $isRightScalar) { + /* + * In Json-Schema mathematically equal numbers are compared equal + */ + if ($isLeftNumber && $isRightNumber && (float) $left === (float) $right) { + return true; + } + return $left === $right; } diff --git a/tests/Constraints/ConstTest.php b/tests/Constraints/ConstTest.php index 6b4cfc1f..d9801906 100644 --- a/tests/Constraints/ConstTest.php +++ b/tests/Constraints/ConstTest.php @@ -17,7 +17,7 @@ class ConstTest extends BaseTestCase public function getInvalidTests(): array { return [ - [ + 'Object with inner string value' => [ '{"value":"foo"}', '{ "type":"object", @@ -27,7 +27,7 @@ public function getInvalidTests(): array "additionalProperties":false }' ], - [ + 'Object with inner integer value' => [ '{"value":5}', '{ "type":"object", @@ -37,7 +37,7 @@ public function getInvalidTests(): array "additionalProperties":false }' ], - [ + 'Object with inner boolean value' => [ '{"value":false}', '{ "type":"object", @@ -47,7 +47,7 @@ public function getInvalidTests(): array "additionalProperties":false }' ], - [ + 'Object with inner numerical string value' => [ '{ "value": { "foo": "12" @@ -71,7 +71,7 @@ public function getInvalidTests(): array public function getValidTests(): array { return [ - [ + 'String value' => [ '{"value":"bar"}', '{ "type":"object", @@ -81,7 +81,7 @@ public function getValidTests(): array "additionalProperties":false }' ], - [ + 'Boolean(false) value' => [ '{"value":false}', '{ "type":"object", @@ -91,7 +91,7 @@ public function getValidTests(): array "additionalProperties":false }' ], - [ + 'Boolean(true) value' => [ '{"value":true}', '{ "type":"object", @@ -101,7 +101,7 @@ public function getValidTests(): array "additionalProperties":false }' ], - [ + 'Integer value' => [ '{"value":5}', '{ "type":"object", @@ -111,7 +111,7 @@ public function getValidTests(): array "additionalProperties":false }' ], - [ + 'Object with inner integer value' => [ '{ "value": { "foo": 12 diff --git a/tests/Constraints/EnumTest.php b/tests/Constraints/EnumTest.php index 97ba611d..a70f871f 100644 --- a/tests/Constraints/EnumTest.php +++ b/tests/Constraints/EnumTest.php @@ -176,7 +176,7 @@ public function getValidTests(): array } }' ], - 'Numeric values with mathematical equality are considered valid' => [ + 'Number values with mathematical equality are considered valid' => [ 'data' => '12', 'schema' => '{ "type": "any", @@ -184,6 +184,14 @@ public function getValidTests(): array 12.0 ] }' + ], + 'Array with number values with mathematical equality are considered valid' => [ + 'input' => '[ 0.0 ]', + 'schema' => '{ + "enum": [ + [ 0 ] + ] + }', ] ]; }