Skip to content

Commit ef462e7

Browse files
committed
Revert "strict object type checking for enum and const"
This reverts commit 4c4cd76.
1 parent 4c4cd76 commit ef462e7

File tree

11 files changed

+34
-180
lines changed

11 files changed

+34
-180
lines changed

.idea/php-test-framework.xml

Lines changed: 0 additions & 14 deletions
This file was deleted.

.idea/php.xml

Lines changed: 0 additions & 56 deletions
This file was deleted.

composer.json

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@
2828
],
2929
"require": {
3030
"php": ">=5.3.3",
31-
"marc-mabe/php-enum": "2.3.1",
32-
"icecave/parity": "1.0.0"
31+
"marc-mabe/php-enum": "2.3.1"
3332
},
3433
"require-dev": {
3534
"friendsofphp/php-cs-fixer": "^2.1",
@@ -74,9 +73,5 @@
7473
"style-fix": "php-cs-fixer fix --verbose",
7574
"test": "phpunit",
7675
"testOnly": "phpunit --colors --filter"
77-
},
78-
"config": {
79-
"disable-tls": true,
80-
"secure-http": false
81-
}
76+
}
8277
}

src/JsonSchema/Constraints/BaseConstraint.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public function addErrors(array $errors)
8383
$errorMask |= $error['context'];
8484
}
8585
});
86-
}
86+
}
8787
}
8888

8989
public function getErrors($errorContext = Validator::ERROR_ALL)

src/JsonSchema/Constraints/ConstConstraint.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
use JsonSchema\ConstraintError;
1313
use JsonSchema\Entity\JsonPointer;
14-
use Icecave\Parity\Parity;
1514

1615
/**
1716
* The ConstConstraint Constraints, validates an element against a constant value
@@ -25,7 +24,6 @@ class ConstConstraint extends Constraint
2524
*/
2625
public function check(&$element, $schema = null, JsonPointer $path = null, $i = null)
2726
{
28-
2927
// Only validate const if the attribute exists
3028
if ($element instanceof UndefinedConstraint && (!isset($schema->required) || !$schema->required)) {
3129
return;
@@ -36,15 +34,21 @@ public function check(&$element, $schema = null, JsonPointer $path = null, $i =
3634
$constType = gettype($const);
3735

3836
if ($this->factory->getConfig(self::CHECK_MODE_TYPE_CAST) && $type == 'array' && $constType == 'object') {
39-
if (Parity::isEqualTo((object) $element, $const)) {
40-
return;
37+
if ((object) $element == $const) {
38+
return;
4139
}
4240
}
4341

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

48-
$this->addError(ConstraintError::CONSTANT(), $path, array('const' => true));
52+
$this->addError(ConstraintError::CONSTANT(), $path, array('const' => $schema->const));
4953
}
5054
}

src/JsonSchema/Constraints/EnumConstraint.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
use JsonSchema\ConstraintError;
1313
use JsonSchema\Entity\JsonPointer;
14-
use Icecave\Parity\Parity;
1514

1615
/**
1716
* The EnumConstraint Constraints, validates an element against a given set of possibilities
@@ -35,15 +34,19 @@ public function check(&$element, $schema = null, JsonPointer $path = null, $i =
3534
foreach ($schema->enum as $enum) {
3635
$enumType = gettype($enum);
3736
if ($this->factory->getConfig(self::CHECK_MODE_TYPE_CAST) && $type == 'array' && $enumType == 'object') {
38-
if (Parity::isEqualTo((object) $element, $enum)) {
39-
return;
40-
}
37+
if ((object) $element == $enum) {
38+
return;
39+
}
4140
}
4241

4342
if ($type === gettype($enum)) {
44-
if(Parity::isEqualTo($element, $enum)){
45-
return;
46-
}
43+
if ($type == 'object') {
44+
if ($element == $enum) {
45+
return;
46+
}
47+
} elseif ($element === $enum) {
48+
return;
49+
}
4750
}
4851
}
4952

src/JsonSchema/Constraints/Factory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class Factory
4444
/**
4545
* @var int Validation context
4646
*/
47-
protected $errorContext = 1;
47+
protected $errorContext = Validator::ERROR_DOCUMENT_VALIDATION;
4848

4949
/**
5050
* @var array
@@ -216,6 +216,6 @@ public function getErrorContext()
216216
*/
217217
public function setErrorContext($errorContext)
218218
{
219-
$this->errorContext = 1;
219+
$this->errorContext = $errorContext;
220220
}
221221
}

tests/Constraints/BaseTestCase.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ abstract class BaseTestCase extends VeryBaseTestCase
2828
*/
2929
public function testInvalidCases($input, $schema, $checkMode = Constraint::CHECK_MODE_NORMAL, $errors = array())
3030
{
31-
return;
3231
$checkMode = $checkMode === null ? Constraint::CHECK_MODE_NORMAL : $checkMode;
3332
if ($this->validateSchema) {
3433
$checkMode |= Constraint::CHECK_MODE_VALIDATE_SCHEMA;
@@ -90,7 +89,6 @@ public function testInvalidCasesUsingAssoc($input, $schema, $checkMode = Constra
9089
*/
9190
public function testValidCases($input, $schema, $checkMode = Constraint::CHECK_MODE_NORMAL)
9291
{
93-
return;
9492
if ($this->validateSchema) {
9593
$checkMode |= Constraint::CHECK_MODE_VALIDATE_SCHEMA;
9694
}
@@ -113,7 +111,6 @@ public function testValidCases($input, $schema, $checkMode = Constraint::CHECK_M
113111
*/
114112
public function testValidCasesUsingAssoc($input, $schema, $checkMode = Constraint::CHECK_MODE_TYPE_CAST)
115113
{
116-
return;
117114
if ($this->validateSchema) {
118115
$checkMode |= Constraint::CHECK_MODE_VALIDATE_SCHEMA;
119116
}

tests/Constraints/ConstTest.php

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -47,24 +47,6 @@ 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-
)
6850
);
6951
}
7052

@@ -111,24 +93,6 @@ public function getValidTests()
11193
"additionalProperties":false
11294
}'
11395
),
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-
)
13296
);
13397
}
13498
}

tests/Constraints/EnumTest.php

Lines changed: 7 additions & 45 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,31 +66,7 @@ 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-
}'
93-
)
69+
)
9470
);
9571
}
9672

@@ -152,28 +128,14 @@ public function getValidTests()
152128
"additionalProperties": false
153129
}'
154130
),
155-
array(
156-
'{
157-
"value": {
158-
"foo": 12
159-
}
160-
}',
131+
array(
132+
'{"value": {"foo": 12}}',
161133
'{
162134
"type": "object",
163135
"properties": {
164-
"value": {
165-
"type": "any",
166-
"enum": [
167-
6,
168-
"foo",
169-
[],
170-
true,
171-
{
172-
"foo": 12
173-
}
174-
]
175-
}
176-
}
136+
"value": {"type": "any", "enum": [6, "foo", [], true, {"foo": 12}]}
137+
},
138+
"additionalProperties": false
177139
}'
178140
)
179141
);

tests/Constraints/FactoryTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ 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'),
7675
array('format', 'JsonSchema\Constraints\FormatConstraint'),
7776
array('schema', 'JsonSchema\Constraints\SchemaConstraint'),
7877
);

0 commit comments

Comments
 (0)