Skip to content

fix: Array with number values with mathematical equality are considered valid #813

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 3 commits into from
Mar 31, 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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
9 changes: 9 additions & 0 deletions src/JsonSchema/Tool/DeepComparer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
18 changes: 9 additions & 9 deletions tests/Constraints/ConstTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ConstTest extends BaseTestCase
public function getInvalidTests(): array
{
return [
[
'Object with inner string value' => [
'{"value":"foo"}',
'{
"type":"object",
Expand All @@ -27,7 +27,7 @@ public function getInvalidTests(): array
"additionalProperties":false
}'
],
[
'Object with inner integer value' => [
'{"value":5}',
'{
"type":"object",
Expand All @@ -37,7 +37,7 @@ public function getInvalidTests(): array
"additionalProperties":false
}'
],
[
'Object with inner boolean value' => [
'{"value":false}',
'{
"type":"object",
Expand All @@ -47,7 +47,7 @@ public function getInvalidTests(): array
"additionalProperties":false
}'
],
[
'Object with inner numerical string value' => [
'{
"value": {
"foo": "12"
Expand All @@ -71,7 +71,7 @@ public function getInvalidTests(): array
public function getValidTests(): array
{
return [
[
'String value' => [
'{"value":"bar"}',
'{
"type":"object",
Expand All @@ -81,7 +81,7 @@ public function getValidTests(): array
"additionalProperties":false
}'
],
[
'Boolean(false) value' => [
'{"value":false}',
'{
"type":"object",
Expand All @@ -91,7 +91,7 @@ public function getValidTests(): array
"additionalProperties":false
}'
],
[
'Boolean(true) value' => [
'{"value":true}',
'{
"type":"object",
Expand All @@ -101,7 +101,7 @@ public function getValidTests(): array
"additionalProperties":false
}'
],
[
'Integer value' => [
'{"value":5}',
'{
"type":"object",
Expand All @@ -111,7 +111,7 @@ public function getValidTests(): array
"additionalProperties":false
}'
],
[
'Object with inner integer value' => [
'{
"value": {
"foo": 12
Expand Down
10 changes: 9 additions & 1 deletion tests/Constraints/EnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,14 +176,22 @@ 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",
"enum": [
12.0
]
}'
],
'Array with number values with mathematical equality are considered valid' => [
'input' => '[ 0.0 ]',
'schema' => '{
"enum": [
[ 0 ]
]
}',
]
];
}
Expand Down