Skip to content

Commit 1bc3db3

Browse files
committed
fix: ensure numeric issues in const are correctly evaluated
Fixes #778
1 parent fbb4049 commit 1bc3db3

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

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)