Skip to content

Commit

Permalink
Merge pull request #518 from shmax/enum-const-fix
Browse files Browse the repository at this point in the history
Strict Enum/Const Object Checking
  • Loading branch information
bighappyface authored Jun 11, 2018
2 parents 52086d6 + 096596c commit 505b5e7
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 22 deletions.
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 is_bool($val) ? var_export($val, true) : $val;
}

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

0 comments on commit 505b5e7

Please sign in to comment.