From a71086d68b0c19d304d3eff047bbf0097d44b67f Mon Sep 17 00:00:00 2001 From: samkeen Date: Fri, 5 Jun 2015 12:21:41 -0700 Subject: [PATCH] For missing properties, the key for that error in set to "" - Expected bahavior is that the key would be set to the poperty name. - added tests to ensure the "property" is populated regardless of required attribute is completely missing or has empty value. --- .../Constraints/UndefinedConstraint.php | 2 +- .../Constraints/RequiredPropertyTest.php | 64 +++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) diff --git a/src/JsonSchema/Constraints/UndefinedConstraint.php b/src/JsonSchema/Constraints/UndefinedConstraint.php index 8527b9db..682b4209 100644 --- a/src/JsonSchema/Constraints/UndefinedConstraint.php +++ b/src/JsonSchema/Constraints/UndefinedConstraint.php @@ -122,7 +122,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($path, "the property " . $required . " is required"); + $this->addError($required, "the property " . $required . " is 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 c1495622..a18afea7 100644 --- a/tests/JsonSchema/Tests/Constraints/RequiredPropertyTest.php +++ b/tests/JsonSchema/Tests/Constraints/RequiredPropertyTest.php @@ -9,8 +9,72 @@ namespace JsonSchema\Tests\Constraints; +use JsonSchema\Constraints\UndefinedConstraint; + class RequiredPropertyTest extends BaseTestCase { + + public function testErrorPropertyIsPopulatedForRequiredIfMissingInInput() + { + $validator = new UndefinedConstraint(); + $document = json_decode( + '{ + "bar": 42 + }' + ); + $schema = json_decode( + '{ + "type": "object", + "properties": { + "foo": {"type": "number"}, + "bar": {"type": "number"} + }, + "required": ["foo"] + }' + ); + + $validator->check($document, $schema); + $error = $validator->getErrors(); + $this->assertErrorHasExpectedPropertyValue($error, "foo"); + } + + public function testErrorPropertyIsPopulatedForRequiredIfEmptyValueInInput() + { + $validator = new UndefinedConstraint(); + $document = json_decode( + '{ + "bar": 42, + "foo": null + }' + ); + $schema = json_decode( + '{ + "type": "object", + "properties": { + "foo": {"type": "number"}, + "bar": {"type": "number"} + }, + "required": ["foo"] + }' + ); + + $validator->check($document, $schema); + $error = $validator->getErrors(); + $this->assertErrorHasExpectedPropertyValue($error, "foo"); + } + + protected function assertErrorHasExpectedPropertyValue($error, $propertyValue) + { + if (!(isset($error[0]) && is_array($error[0]) && isset($error[0]['property']))) { + $this->fail( + "Malformed error response. Expected to have subset in form: array(0 => array('property' => )))" + . " . Error response was: " . json_encode($error) + ); + } + $this->assertEquals($propertyValue, $error[0]['property']); + + } + public function getInvalidTests() { return array(