Skip to content

Commit

Permalink
Merge pull request #204 from JustTSK/master
Browse files Browse the repository at this point in the history
Fix path output for required properties
  • Loading branch information
bighappyface committed Dec 14, 2015
2 parents 8abfcaa + 505e6f3 commit f791815
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/JsonSchema/Constraints/UndefinedConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ protected function validateCommonProperties($value, $schema = null, $path = null
// Draft 4 - Required is an array of strings - e.g. "required": ["foo", ...]
foreach ($schema->required as $required) {
if (!property_exists($value, $required)) {
$this->addError($required, "The property " . $required . " is required", 'required');
$this->addError((!$path) ? $required : "$path.$required", "The property " . $required . " is required", 'required');
}
}
} else if (isset($schema->required) && !is_array($schema->required)) {
Expand Down
33 changes: 33 additions & 0 deletions tests/JsonSchema/Tests/Constraints/RequiredPropertyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,39 @@ public function testErrorPropertyIsPopulatedForRequiredIfMissingInInput()
$this->assertErrorHasExpectedPropertyValue($error, "foo");
}

public function testPathErrorPropertyIsPopulatedForRequiredIfMissingInInput()
{
$validator = new UndefinedConstraint();
$document = json_decode(
'{
"foo": [{"baz": 1.5}]
}'
);
$schema = json_decode(
'{
"type": "object",
"properties": {
"foo": {
"type": "array",
"items": {
"type": "object",
"properties": {
"bar": {"type": "number"},
"baz": {"type": "number"}
},
"required": ["bar"]
}
}
},
"required": ["foo"]
}'
);

$validator->check($document, $schema);
$error = $validator->getErrors();
$this->assertErrorHasExpectedPropertyValue($error, "foo[0].bar");
}

public function testErrorPropertyIsPopulatedForRequiredIfEmptyValueInInput()
{
$validator = new UndefinedConstraint();
Expand Down

0 comments on commit f791815

Please sign in to comment.