Skip to content

fix: ensure numeric issues in const are correctly evaluated #805

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
Expand Down
20 changes: 12 additions & 8 deletions src/JsonSchema/Constraints/EnumConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
19 changes: 14 additions & 5 deletions tests/Constraints/EnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,19 +162,28 @@ public function getValidTests(): array
"type": "object",
"properties": {
"value": {
"type": "any",
"type": "any",
"enum": [
6,
"foo",
[],
true,
6,
"foo",
[],
true,
{
"foo": 12
}
]
}
}
}'
],
'Numeric values with mathematical equality are considered valid' => [
'data' => '12',
'schema' => '{
"type": "any",
"enum": [
12.0
]
}'
]
];
}
Expand Down