Skip to content

Commit 505b5e7

Browse files
authored
Merge pull request #518 from shmax/enum-const-fix
Strict Enum/Const Object Checking
2 parents 52086d6 + 096596c commit 505b5e7

File tree

7 files changed

+90
-22
lines changed

7 files changed

+90
-22
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
],
2929
"require": {
3030
"php": ">=5.3.3",
31-
"marc-mabe/php-enum": "2.3.1"
31+
"marc-mabe/php-enum": "2.3.1",
32+
"icecave/parity": "1.0.0"
3233
},
3334
"require-dev": {
3435
"friendsofphp/php-cs-fixer": "^2.1",

src/JsonSchema/Constraints/BaseConstraint.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function addError(ConstraintError $constraint, JsonPointer $path = null,
5353
'pointer' => ltrim(strval($path ?: new JsonPointer('')), '#'),
5454
'message' => ucfirst(vsprintf($message, array_map(function ($val) {
5555
if (is_scalar($val)) {
56-
return $val;
56+
return is_bool($val) ? var_export($val, true) : $val;
5757
}
5858

5959
return json_encode($val);

src/JsonSchema/Constraints/ConstConstraint.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace JsonSchema\Constraints;
1111

12+
use Icecave\Parity\Parity;
1213
use JsonSchema\ConstraintError;
1314
use JsonSchema\Entity\JsonPointer;
1415

@@ -34,19 +35,13 @@ public function check(&$element, $schema = null, JsonPointer $path = null, $i =
3435
$constType = gettype($const);
3536

3637
if ($this->factory->getConfig(self::CHECK_MODE_TYPE_CAST) && $type == 'array' && $constType == 'object') {
37-
if ((object) $element == $const) {
38+
if (Parity::isEqualTo((object) $element, $const)) {
3839
return;
3940
}
4041
}
4142

42-
if ($type === gettype($const)) {
43-
if ($type == 'object') {
44-
if ($element == $const) {
45-
return;
46-
}
47-
} elseif ($element === $const) {
48-
return;
49-
}
43+
if (Parity::isEqualTo($element, $const)) {
44+
return;
5045
}
5146

5247
$this->addError(ConstraintError::CONSTANT(), $path, array('const' => $schema->const));

src/JsonSchema/Constraints/EnumConstraint.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace JsonSchema\Constraints;
1111

12+
use Icecave\Parity\Parity;
1213
use JsonSchema\ConstraintError;
1314
use JsonSchema\Entity\JsonPointer;
1415

@@ -34,17 +35,13 @@ public function check(&$element, $schema = null, JsonPointer $path = null, $i =
3435
foreach ($schema->enum as $enum) {
3536
$enumType = gettype($enum);
3637
if ($this->factory->getConfig(self::CHECK_MODE_TYPE_CAST) && $type == 'array' && $enumType == 'object') {
37-
if ((object) $element == $enum) {
38+
if (Parity::isEqualTo((object) $element, $enum)) {
3839
return;
3940
}
4041
}
4142

4243
if ($type === gettype($enum)) {
43-
if ($type == 'object') {
44-
if ($element == $enum) {
45-
return;
46-
}
47-
} elseif ($element === $enum) {
44+
if (Parity::isEqualTo($element, $enum)) {
4845
return;
4946
}
5047
}

tests/Constraints/ConstTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,24 @@ public function getInvalidTests()
4747
"additionalProperties":false
4848
}'
4949
),
50+
array(
51+
'{
52+
"value": {
53+
"foo": "12"
54+
}
55+
}',
56+
'{
57+
"type": "object",
58+
"properties": {
59+
"value": {
60+
"type": "any",
61+
"const": {
62+
"foo": 12
63+
}
64+
}
65+
}
66+
}'
67+
)
5068
);
5169
}
5270

@@ -93,6 +111,24 @@ public function getValidTests()
93111
"additionalProperties":false
94112
}'
95113
),
114+
array(
115+
'{
116+
"value": {
117+
"foo": 12
118+
}
119+
}',
120+
'{
121+
"type": "object",
122+
"properties": {
123+
"value": {
124+
"type": "any",
125+
"const": {
126+
"foo": 12
127+
}
128+
}
129+
}
130+
}'
131+
)
96132
);
97133
}
98134
}

tests/Constraints/EnumTest.php

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function getInvalidTests()
4444
}'
4545
),
4646
array(
47-
'{"value": 4}',
47+
'{"value": "4"}',
4848
'{
4949
"type": "object",
5050
"properties": {
@@ -66,6 +66,30 @@ public function getInvalidTests()
6666
},
6767
"additionalProperties": false
6868
}'
69+
),
70+
array(
71+
'{
72+
"value": {
73+
"foo": "12"
74+
}
75+
}',
76+
'{
77+
"type": "object",
78+
"properties": {
79+
"value": {
80+
"type": "any",
81+
"enum": [
82+
6,
83+
"foo",
84+
[],
85+
true,
86+
{
87+
"foo": 12
88+
}
89+
]
90+
}
91+
}
92+
}'
6993
)
7094
);
7195
}
@@ -129,13 +153,27 @@ public function getValidTests()
129153
}'
130154
),
131155
array(
132-
'{"value": {"foo": 12}}',
156+
'{
157+
"value": {
158+
"foo": 12
159+
}
160+
}',
133161
'{
134162
"type": "object",
135163
"properties": {
136-
"value": {"type": "any", "enum": [6, "foo", [], true, {"foo": 12}]}
137-
},
138-
"additionalProperties": false
164+
"value": {
165+
"type": "any",
166+
"enum": [
167+
6,
168+
"foo",
169+
[],
170+
true,
171+
{
172+
"foo": 12
173+
}
174+
]
175+
}
176+
}
139177
}'
140178
)
141179
);

tests/Constraints/FactoryTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public function constraintNameProvider()
7272
array('string', 'JsonSchema\Constraints\StringConstraint'),
7373
array('number', 'JsonSchema\Constraints\NumberConstraint'),
7474
array('enum', 'JsonSchema\Constraints\EnumConstraint'),
75+
array('const', 'JsonSchema\Constraints\ConstConstraint'),
7576
array('format', 'JsonSchema\Constraints\FormatConstraint'),
7677
array('schema', 'JsonSchema\Constraints\SchemaConstraint'),
7778
);

0 commit comments

Comments
 (0)