diff --git a/src/JsonSchema/Constraints/UndefinedConstraint.php b/src/JsonSchema/Constraints/UndefinedConstraint.php index 658b1b7a..c0337200 100644 --- a/src/JsonSchema/Constraints/UndefinedConstraint.php +++ b/src/JsonSchema/Constraints/UndefinedConstraint.php @@ -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)) { diff --git a/tests/JsonSchema/Tests/Constraints/RequiredPropertyTest.php b/tests/JsonSchema/Tests/Constraints/RequiredPropertyTest.php index a18afea7..fd3b4193 100644 --- a/tests/JsonSchema/Tests/Constraints/RequiredPropertyTest.php +++ b/tests/JsonSchema/Tests/Constraints/RequiredPropertyTest.php @@ -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();