Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
19 changes: 10 additions & 9 deletions src/JsonSchema/Constraints/Undefined.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,22 +109,23 @@ protected function validateCommonProperties($value, $schema = null, $path = null

// Verify required values
if (is_object($value)) {
if ($value instanceof Undefined) {
// Draft 3 - Required attribute - e.g. "foo": {"type": "string", "required": true}
if (isset($schema->required) && $schema->required) {
$this->addError($path, "is missing and it is required");
}
} else if (isset($schema->required)) {
if (isset($schema->required) && is_array($schema->required) ) {
// Draft 4 - Required is an array of strings - e.g. "required": ["foo", ...]
foreach ($schema->required as $required) {
if (!property_exists($value, $required)) {
$this->addError($path, "the property " . $required . " is required");
}
}
} else {
$this->checkType($value, $schema, $path);
} else if (isset($schema->required)) {
// Draft 3 - Required attribute - e.g. "foo": {"type": "string", "required": true}
if ( $schema->required && $value instanceof Undefined) {
$this->addError($path, "is missing and it is required");
}
}
} else {
}

// Verify type
if (!($value instanceof Undefined)) {
$this->checkType($value, $schema, $path);
}

Expand Down
87 changes: 87 additions & 0 deletions tests/JsonSchema/Tests/Constraints/RequiredPropertyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,82 @@ public function getInvalidTests()
'{
"required": ["foo"]
}'
),
array(
'{
}',
'{
"type": "object",
"properties": {
"foo": { "required": true }
}
}'
),
array(
'{
"string":{}
}',
'{
"type":"object",
"properties": {
"string":{"type":"string", "required": true}
}
}'
),
array(
'{
"number":{}
}',
'{
"type":"object",
"properties": {
"number":{"type":"number", "required": true}
}
}'
),
array(
'{
"integer":{}
}',
'{
"type":"object",
"properties": {
"integer":{"type":"integer", "required": true}
}
}'
),
array(
'{
"boolean":{}
}',
'{
"type":"object",
"properties": {
"boolean":{"type":"boolean", "required": true}
}
}'
),
array(
'{
"array":{}
}',
'{
"type":"object",
"properties": {
"array":{"type":"array", "required": true}
}
}'
),
array(
'{
"null":{}
}',
'{
"type":"object",
"properties": {
"null":{"type":"null", "required": true}
}
}'
)
);
}
Expand Down Expand Up @@ -179,6 +255,17 @@ public function getValidTests()
},
"required": ["foo"]
}'
),
array(
'{
"foo": {}
}',
'{
"type": "object",
"properties": {
"foo": { "required": true }
}
}'
)
);
}
Expand Down