Skip to content

Commit 470306a

Browse files
committed
fix: Array with number values with mathematical equality are considered valid
1 parent f769861 commit 470306a

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

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)