diff --git a/CHANGELOG.md b/CHANGELOG.md index a22075eb..4f799e49 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Fixed +- ensure numeric issues in const are correctly evaluated ([#805](https://github.com/jsonrainbow/json-schema/pull/805)) ## [6.3.0] - 2025-03-14 ### Fixed @@ -15,7 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Changed - replace icecave/parity with custom deep comparator ([#803](https://github.com/jsonrainbow/json-schema/pull/803)) -- + ## [6.2.1] - 2025-03-06 ### Fixed - allow items: true to pass validation ([#801](https://github.com/jsonrainbow/json-schema/pull/801)) diff --git a/src/JsonSchema/Constraints/EnumConstraint.php b/src/JsonSchema/Constraints/EnumConstraint.php index ce3853d6..21096385 100644 --- a/src/JsonSchema/Constraints/EnumConstraint.php +++ b/src/JsonSchema/Constraints/EnumConstraint.php @@ -37,16 +37,20 @@ public function check(&$element, $schema = null, ?JsonPointer $path = null, $i = foreach ($schema->enum as $enum) { $enumType = gettype($enum); - if ($this->factory->getConfig(self::CHECK_MODE_TYPE_CAST) && $type === 'array' && $enumType === 'object') { - if (DeepComparer::isEqual((object) $element, $enum)) { - return; - } + if ($enumType === 'object' + && $type === 'array' + && $this->factory->getConfig(self::CHECK_MODE_TYPE_CAST) + && DeepComparer::isEqual((object) $element, $enum) + ) { + return; } - if ($type === gettype($enum)) { - if (DeepComparer::isEqual($element, $enum)) { - return; - } + if (($type === $enumType) && DeepComparer::isEqual($element, $enum)) { + return; + } + + if (is_numeric($element) && is_numeric($enum) && DeepComparer::isEqual((float) $element, (float) $enum)) { + return; } } diff --git a/tests/Constraints/EnumTest.php b/tests/Constraints/EnumTest.php index 2ff14147..97ba611d 100644 --- a/tests/Constraints/EnumTest.php +++ b/tests/Constraints/EnumTest.php @@ -162,12 +162,12 @@ public function getValidTests(): array "type": "object", "properties": { "value": { - "type": "any", + "type": "any", "enum": [ - 6, - "foo", - [], - true, + 6, + "foo", + [], + true, { "foo": 12 } @@ -175,6 +175,15 @@ public function getValidTests(): array } } }' + ], + 'Numeric values with mathematical equality are considered valid' => [ + 'data' => '12', + 'schema' => '{ + "type": "any", + "enum": [ + 12.0 + ] + }' ] ]; }