Skip to content
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

Strict Enum/Const Object Checking #518

Merged
merged 7 commits into from
Jun 11, 2018
Merged
Show file tree
Hide file tree
Changes from 5 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
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
],
"require": {
"php": ">=5.3.3",
"marc-mabe/php-enum": "2.3.1"
"marc-mabe/php-enum": "2.3.1",
"icecave/parity": "1.0.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.1",
Expand Down
2 changes: 1 addition & 1 deletion src/JsonSchema/Constraints/BaseConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function addError(ConstraintError $constraint, JsonPointer $path = null,
'pointer' => ltrim(strval($path ?: new JsonPointer('')), '#'),
'message' => ucfirst(vsprintf($message, array_map(function ($val) {
if (is_scalar($val)) {
return $val;
return var_export($val, true);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra credit: I noticed that the const error string was producing this message for bool type:

Does not have a value equal to 1

instead of

Does not have a value equal to true

}

return json_encode($val);
Expand Down
13 changes: 4 additions & 9 deletions src/JsonSchema/Constraints/ConstConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace JsonSchema\Constraints;

use Icecave\Parity\Parity;
use JsonSchema\ConstraintError;
use JsonSchema\Entity\JsonPointer;

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

if ($this->factory->getConfig(self::CHECK_MODE_TYPE_CAST) && $type == 'array' && $constType == 'object') {
if ((object) $element == $const) {
if (Parity::isEqualTo((object) $element, $const)) {
return;
}
}

if ($type === gettype($const)) {
if ($type == 'object') {
if ($element == $const) {
return;
}
} elseif ($element === $const) {
return;
}
if (Parity::isEqualTo($element, $const)) {
return;
}

$this->addError(ConstraintError::CONSTANT(), $path, array('const' => $schema->const));
Expand Down
9 changes: 3 additions & 6 deletions src/JsonSchema/Constraints/EnumConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace JsonSchema\Constraints;

use Icecave\Parity\Parity;
use JsonSchema\ConstraintError;
use JsonSchema\Entity\JsonPointer;

Expand All @@ -34,17 +35,13 @@ 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 ((object) $element == $enum) {
if (Parity::isEqualTo((object) $element, $enum)) {
return;
}
}

if ($type === gettype($enum)) {
if ($type == 'object') {
if ($element == $enum) {
return;
}
} elseif ($element === $enum) {
if (Parity::isEqualTo($element, $enum)) {
return;
}
}
Expand Down
36 changes: 36 additions & 0 deletions tests/Constraints/ConstTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,24 @@ public function getInvalidTests()
"additionalProperties":false
}'
),
array(
'{
"value": {
"foo": "12"
}
}',
'{
"type": "object",
"properties": {
"value": {
"type": "any",
"const": {
"foo": 12
}
}
}
}'
)
);
}

Expand Down Expand Up @@ -93,6 +111,24 @@ public function getValidTests()
"additionalProperties":false
}'
),
array(
'{
"value": {
"foo": 12
}
}',
'{
"type": "object",
"properties": {
"value": {
"type": "any",
"const": {
"foo": 12
}
}
}
}'
)
);
}
}
48 changes: 43 additions & 5 deletions tests/Constraints/EnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function getInvalidTests()
}'
),
array(
'{"value": 4}',
'{"value": "4"}',
'{
"type": "object",
"properties": {
Expand All @@ -66,6 +66,30 @@ public function getInvalidTests()
},
"additionalProperties": false
}'
),
array(
'{
"value": {
"foo": "12"
}
}',
'{
"type": "object",
"properties": {
"value": {
"type": "any",
"enum": [
6,
"foo",
[],
true,
{
"foo": 12
}
]
}
}
}'
)
);
}
Expand Down Expand Up @@ -129,13 +153,27 @@ public function getValidTests()
}'
),
array(
'{"value": {"foo": 12}}',
'{
"value": {
"foo": 12
}
}',
'{
"type": "object",
"properties": {
"value": {"type": "any", "enum": [6, "foo", [], true, {"foo": 12}]}
},
"additionalProperties": false
"value": {
"type": "any",
"enum": [
6,
"foo",
[],
true,
{
"foo": 12
}
]
}
}
}'
)
);
Expand Down
1 change: 1 addition & 0 deletions tests/Constraints/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public function constraintNameProvider()
array('string', 'JsonSchema\Constraints\StringConstraint'),
array('number', 'JsonSchema\Constraints\NumberConstraint'),
array('enum', 'JsonSchema\Constraints\EnumConstraint'),
array('const', 'JsonSchema\Constraints\ConstConstraint'),
array('format', 'JsonSchema\Constraints\FormatConstraint'),
array('schema', 'JsonSchema\Constraints\SchemaConstraint'),
);
Expand Down