Skip to content

Commit b1c2f65

Browse files
author
samkeen
committed
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.
1 parent 7dfe4f1 commit b1c2f65

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

src/JsonSchema/Constraints/UndefinedConstraint.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ protected function validateCommonProperties($value, $schema = null, $path = null
122122
// Draft 4 - Required is an array of strings - e.g. "required": ["foo", ...]
123123
foreach ($schema->required as $required) {
124124
if (!property_exists($value, $required)) {
125-
$this->addError($path, "the property " . $required . " is required");
125+
$this->addError($required, "the property " . $required . " is required");
126126
}
127127
}
128128
} else if (isset($schema->required) && !is_array($schema->required)) {

tests/JsonSchema/Tests/Constraints/RequiredPropertyTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,60 @@
99

1010
namespace JsonSchema\Tests\Constraints;
1111

12+
use JsonSchema\Constraints\UndefinedConstraint;
13+
1214
class RequiredPropertyTest extends BaseTestCase
1315
{
16+
17+
public function testErrorPropertyIsPopulatedForRequiredIfMissingInInput()
18+
{
19+
$validator = new UndefinedConstraint();
20+
$document = json_decode('{
21+
"bar": 42
22+
}');
23+
$schema = json_decode('{
24+
"type": "object",
25+
"properties": {
26+
"foo": {"type": "number"},
27+
"bar": {"type": "number"}
28+
},
29+
"required": ["foo"]
30+
}');
31+
32+
$validator->check($document, $schema);
33+
$error = $validator->getErrors();
34+
if(!isset($error[0]['property'])) {
35+
$this->fail("Malformed error response. Expected \$error[0]['property'] to be set. Error response was "
36+
. json_encode($error));
37+
}
38+
$this->assertEquals('foo', $error[0]['property']);
39+
}
40+
41+
public function testErrorPropertyIsPopulatedForRequiredIfEmptyValueInInput()
42+
{
43+
$validator = new UndefinedConstraint();
44+
$document = json_decode('{
45+
"bar": 42,
46+
"foo": null
47+
}');
48+
$schema = json_decode('{
49+
"type": "object",
50+
"properties": {
51+
"foo": {"type": "number"},
52+
"bar": {"type": "number"}
53+
},
54+
"required": ["foo"]
55+
}');
56+
57+
$validator->check($document, $schema);
58+
$error = $validator->getErrors();
59+
if(!isset($error[0]['property'])) {
60+
$this->fail("Malformed error response. Expected \$error[0]['property'] to be set. Error response was "
61+
. json_encode($error));
62+
}
63+
$this->assertEquals('foo', $error[0]['property']);
64+
}
65+
1466
public function getInvalidTests()
1567
{
1668
return array(

0 commit comments

Comments
 (0)