Skip to content

Commit e9ac599

Browse files
fix: ensure numeric issues in const are correctly evaluated (#805)
Fixes #778
1 parent fbb4049 commit e9ac599

File tree

3 files changed

+29
-14
lines changed

3 files changed

+29
-14
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

88
## [Unreleased]
9+
### Fixed
10+
- ensure numeric issues in const are correctly evaluated ([#805](https://github.com/jsonrainbow/json-schema/pull/805))
911

1012
## [6.3.0] - 2025-03-14
1113
### Fixed
@@ -15,7 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1517

1618
## Changed
1719
- replace icecave/parity with custom deep comparator ([#803](https://github.com/jsonrainbow/json-schema/pull/803))
18-
-
20+
1921
## [6.2.1] - 2025-03-06
2022
### Fixed
2123
- allow items: true to pass validation ([#801](https://github.com/jsonrainbow/json-schema/pull/801))

src/JsonSchema/Constraints/EnumConstraint.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,20 @@ public function check(&$element, $schema = null, ?JsonPointer $path = null, $i =
3737
foreach ($schema->enum as $enum) {
3838
$enumType = gettype($enum);
3939

40-
if ($this->factory->getConfig(self::CHECK_MODE_TYPE_CAST) && $type === 'array' && $enumType === 'object') {
41-
if (DeepComparer::isEqual((object) $element, $enum)) {
42-
return;
43-
}
40+
if ($enumType === 'object'
41+
&& $type === 'array'
42+
&& $this->factory->getConfig(self::CHECK_MODE_TYPE_CAST)
43+
&& DeepComparer::isEqual((object) $element, $enum)
44+
) {
45+
return;
4446
}
4547

46-
if ($type === gettype($enum)) {
47-
if (DeepComparer::isEqual($element, $enum)) {
48-
return;
49-
}
48+
if (($type === $enumType) && DeepComparer::isEqual($element, $enum)) {
49+
return;
50+
}
51+
52+
if (is_numeric($element) && is_numeric($enum) && DeepComparer::isEqual((float) $element, (float) $enum)) {
53+
return;
5054
}
5155
}
5256

tests/Constraints/EnumTest.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,19 +162,28 @@ public function getValidTests(): array
162162
"type": "object",
163163
"properties": {
164164
"value": {
165-
"type": "any",
165+
"type": "any",
166166
"enum": [
167-
6,
168-
"foo",
169-
[],
170-
true,
167+
6,
168+
"foo",
169+
[],
170+
true,
171171
{
172172
"foo": 12
173173
}
174174
]
175175
}
176176
}
177177
}'
178+
],
179+
'Numeric values with mathematical equality are considered valid' => [
180+
'data' => '12',
181+
'schema' => '{
182+
"type": "any",
183+
"enum": [
184+
12.0
185+
]
186+
}'
178187
]
179188
];
180189
}

0 commit comments

Comments
 (0)