Skip to content

Commit f791815

Browse files
committed
Merge pull request jsonrainbow#204 from JustTSK/master
Fix path output for required properties
2 parents 8abfcaa + 505e6f3 commit f791815

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/JsonSchema/Constraints/UndefinedConstraint.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ protected function validateCommonProperties($value, $schema = null, $path = null
121121
// Draft 4 - Required is an array of strings - e.g. "required": ["foo", ...]
122122
foreach ($schema->required as $required) {
123123
if (!property_exists($value, $required)) {
124-
$this->addError($required, "The property " . $required . " is required", 'required');
124+
$this->addError((!$path) ? $required : "$path.$required", "The property " . $required . " is required", 'required');
125125
}
126126
}
127127
} else if (isset($schema->required) && !is_array($schema->required)) {

tests/JsonSchema/Tests/Constraints/RequiredPropertyTest.php

+33
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,39 @@ public function testErrorPropertyIsPopulatedForRequiredIfMissingInInput()
3838
$this->assertErrorHasExpectedPropertyValue($error, "foo");
3939
}
4040

41+
public function testPathErrorPropertyIsPopulatedForRequiredIfMissingInInput()
42+
{
43+
$validator = new UndefinedConstraint();
44+
$document = json_decode(
45+
'{
46+
"foo": [{"baz": 1.5}]
47+
}'
48+
);
49+
$schema = json_decode(
50+
'{
51+
"type": "object",
52+
"properties": {
53+
"foo": {
54+
"type": "array",
55+
"items": {
56+
"type": "object",
57+
"properties": {
58+
"bar": {"type": "number"},
59+
"baz": {"type": "number"}
60+
},
61+
"required": ["bar"]
62+
}
63+
}
64+
},
65+
"required": ["foo"]
66+
}'
67+
);
68+
69+
$validator->check($document, $schema);
70+
$error = $validator->getErrors();
71+
$this->assertErrorHasExpectedPropertyValue($error, "foo[0].bar");
72+
}
73+
4174
public function testErrorPropertyIsPopulatedForRequiredIfEmptyValueInInput()
4275
{
4376
$validator = new UndefinedConstraint();

0 commit comments

Comments
 (0)