Skip to content

Commit 26ed648

Browse files
committed
Don't run checks which assume a defined instance against undefined
Validation tests for an undefined instance should be limited to those tests which are actually applicable (e.g. required, default). Tests which assume an instance previously attempted to validate an internal default when the instance is undefined, rather than ignoring it, which is incorrect behavior. Closes #566.
1 parent 0cf4d5b commit 26ed648

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/JsonSchema/Constraints/UndefinedConstraint.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,12 @@ protected function validateCommonProperties(&$value, $schema = null, JsonPointer
153153
$propertyName = end($propertyPaths);
154154
$this->addError(ConstraintError::REQUIRED(), $path, array('property' => $propertyName));
155155
}
156+
} else {
157+
// If the value is both undefined and not required, skip remaining checks
158+
// in this method which assume an actual, defined instance when validating.
159+
if ($value instanceof self) {
160+
return;
161+
}
156162
}
157163
}
158164

tests/Constraints/NotTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,23 @@ public function getInvalidTests()
3131
}
3232
}
3333
}'
34+
),
35+
array( // check that a missing, required property is correctly validated
36+
'{"y": "foo"}',
37+
'{
38+
"type": "object",
39+
"required": ["x"],
40+
"properties": {
41+
"x": {
42+
"not": {
43+
"type": "null"
44+
}
45+
}
46+
}
47+
}'
48+
3449
)
50+
3551
);
3652
}
3753

@@ -69,6 +85,20 @@ public function getValidTests()
6985
}
7086
}
7187
}'
88+
),
89+
array( // check that a missing, non-required property isn't validated
90+
'{"y": "foo"}',
91+
'{
92+
"type": "object",
93+
"properties": {
94+
"x": {
95+
"not": {
96+
"type": "null"
97+
}
98+
}
99+
}
100+
}'
101+
72102
)
73103
);
74104
}

0 commit comments

Comments
 (0)