Skip to content

Commit 61059b2

Browse files
committed
refactor conditional logic of 'required' validation
1 parent cdaf685 commit 61059b2

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

src/JsonSchema/Constraints/Undefined.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -109,20 +109,20 @@ protected function validateCommonProperties($value, $schema = null, $path = null
109109

110110
// Verify required values
111111
if (is_object($value)) {
112-
if ($value instanceof Undefined) {
113-
// Draft 3 - Required attribute - e.g. "foo": {"type": "string", "required": true}
114-
if (isset($schema->required) && $schema->required) {
115-
$this->addError($path, "is missing and it is required");
112+
if (isset($schema->required) && is_array($schema->required) ) {
113+
// Draft 4 - Required is an array of strings - e.g. "required": ["foo", ...]
114+
foreach ($schema->required as $required) {
115+
if (!property_exists($value, $required)) {
116+
$this->addError($path, "the property " . $required . " is required");
117+
}
116118
}
117119
} else if (isset($schema->required)) {
118-
if( is_array($schema->required)) {
119-
// Draft 4 - Required is an array of strings - e.g. "required": ["foo", ...]
120-
foreach ($schema->required as $required) {
121-
if (!property_exists($value, $required)) {
122-
$this->addError($path, "the property " . $required . " is required");
123-
}
124-
}
120+
// Draft 3 - Required attribute - e.g. "foo": {"type": "string", "required": true}
121+
if ( $schema->required && $value instanceof Undefined) {
122+
$this->addError($path, "is missing and it is required");
125123
}
124+
} else if ($value instanceof Undefined) {
125+
// don't check type of Undefined value
126126
} else {
127127
$this->checkType($value, $schema, $path);
128128
}

0 commit comments

Comments
 (0)