Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 528b39f

Browse files
fix: Array with number values with mathematical equality are considered valid (#813)
Our Bowtie [report](https://bowtie.report/#/dialects/draft4?language=php) pointed out we are not correctly validating an enum type with an array holding a number type
1 parent 76ba104 commit 528b39f

File tree

4 files changed

+28
-11
lines changed

4 files changed

+28
-11
lines changed

‎CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
- Run PHPStan using the lowest and highest php version ([#811](https://github.com/jsonrainbow/json-schema/pull/811))
1111
### Fixed
1212
- Use parallel-lint and cs2pr for improved feedback on linting errors ([#812](https://github.com/jsonrainbow/json-schema/pull/812))
13+
- Array with number values with mathematical equality are considered valid ([#813](https://github.com/jsonrainbow/json-schema/pull/813))
1314
## Changed
1415
- Correct PHPStan findings in validator ([#808](https://github.com/jsonrainbow/json-schema/pull/808))
1516

16-
1717
## [6.3.1] - 2025年03月18日
1818
### Fixed
1919
- ensure numeric issues in const are correctly evaluated ([#805](https://github.com/jsonrainbow/json-schema/pull/805))

‎src/JsonSchema/Tool/DeepComparer.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,18 @@ public static function isEqual($left, $right): bool
1717
}
1818

1919
$isLeftScalar = is_scalar($left);
20+
$isLeftNumber = is_int($left) || is_float($left);
2021
$isRightScalar = is_scalar($right);
22+
$isRightNumber = is_int($right) || is_float($right);
2123

2224
if ($isLeftScalar && $isRightScalar) {
25+
/*
26+
* In Json-Schema mathematically equal numbers are compared equal
27+
*/
28+
if ($isLeftNumber && $isRightNumber && (float) $left === (float) $right) {
29+
return true;
30+
}
31+
2332
return $left === $right;
2433
}
2534

‎tests/Constraints/ConstTest.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class ConstTest extends BaseTestCase
1717
public function getInvalidTests(): array
1818
{
1919
return [
20-
[
20+
'Object with inner string value' => [
2121
'{"value":"foo"}',
2222
'{
2323
"type":"object",
@@ -27,7 +27,7 @@ public function getInvalidTests(): array
2727
"additionalProperties":false
2828
}'
2929
],
30-
[
30+
'Object with inner integer value' => [
3131
'{"value":5}',
3232
'{
3333
"type":"object",
@@ -37,7 +37,7 @@ public function getInvalidTests(): array
3737
"additionalProperties":false
3838
}'
3939
],
40-
[
40+
'Object with inner boolean value' => [
4141
'{"value":false}',
4242
'{
4343
"type":"object",
@@ -47,7 +47,7 @@ public function getInvalidTests(): array
4747
"additionalProperties":false
4848
}'
4949
],
50-
[
50+
'Object with inner numerical string value' => [
5151
'{
5252
"value": {
5353
"foo": "12"
@@ -71,7 +71,7 @@ public function getInvalidTests(): array
7171
public function getValidTests(): array
7272
{
7373
return [
74-
[
74+
'String value' => [
7575
'{"value":"bar"}',
7676
'{
7777
"type":"object",
@@ -81,7 +81,7 @@ public function getValidTests(): array
8181
"additionalProperties":false
8282
}'
8383
],
84-
[
84+
'Boolean(false) value' => [
8585
'{"value":false}',
8686
'{
8787
"type":"object",
@@ -91,7 +91,7 @@ public function getValidTests(): array
9191
"additionalProperties":false
9292
}'
9393
],
94-
[
94+
'Boolean(true) value' => [
9595
'{"value":true}',
9696
'{
9797
"type":"object",
@@ -101,7 +101,7 @@ public function getValidTests(): array
101101
"additionalProperties":false
102102
}'
103103
],
104-
[
104+
'Integer value' => [
105105
'{"value":5}',
106106
'{
107107
"type":"object",
@@ -111,7 +111,7 @@ public function getValidTests(): array
111111
"additionalProperties":false
112112
}'
113113
],
114-
[
114+
'Object with inner integer value' => [
115115
'{
116116
"value": {
117117
"foo": 12

‎tests/Constraints/EnumTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,14 +176,22 @@ public function getValidTests(): array
176176
}
177177
}'
178178
],
179-
'Numeric values with mathematical equality are considered valid' => [
179+
'Number values with mathematical equality are considered valid' => [
180180
'data' => '12',
181181
'schema' => '{
182182
"type": "any",
183183
"enum": [
184184
12.0
185185
]
186186
}'
187+
],
188+
'Array with number values with mathematical equality are considered valid' => [
189+
'input' => '[ 0.0 ]',
190+
'schema' => '{
191+
"enum": [
192+
[ 0 ]
193+
]
194+
}',
187195
]
188196
];
189197
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /