Skip to content

Commit 66c1330

Browse files
committed
Merge pull request #154 from samkeen/fix_empty_property_keyname_when_property_is_missing
For missing properties, the key for that error in set to ""
2 parents b398411 + a71086d commit 66c1330

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-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: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,72 @@
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+
'{
22+
"bar": 42
23+
}'
24+
);
25+
$schema = json_decode(
26+
'{
27+
"type": "object",
28+
"properties": {
29+
"foo": {"type": "number"},
30+
"bar": {"type": "number"}
31+
},
32+
"required": ["foo"]
33+
}'
34+
);
35+
36+
$validator->check($document, $schema);
37+
$error = $validator->getErrors();
38+
$this->assertErrorHasExpectedPropertyValue($error, "foo");
39+
}
40+
41+
public function testErrorPropertyIsPopulatedForRequiredIfEmptyValueInInput()
42+
{
43+
$validator = new UndefinedConstraint();
44+
$document = json_decode(
45+
'{
46+
"bar": 42,
47+
"foo": null
48+
}'
49+
);
50+
$schema = json_decode(
51+
'{
52+
"type": "object",
53+
"properties": {
54+
"foo": {"type": "number"},
55+
"bar": {"type": "number"}
56+
},
57+
"required": ["foo"]
58+
}'
59+
);
60+
61+
$validator->check($document, $schema);
62+
$error = $validator->getErrors();
63+
$this->assertErrorHasExpectedPropertyValue($error, "foo");
64+
}
65+
66+
protected function assertErrorHasExpectedPropertyValue($error, $propertyValue)
67+
{
68+
if (!(isset($error[0]) && is_array($error[0]) && isset($error[0]['property']))) {
69+
$this->fail(
70+
"Malformed error response. Expected to have subset in form: array(0 => array('property' => <value>)))"
71+
. " . Error response was: " . json_encode($error)
72+
);
73+
}
74+
$this->assertEquals($propertyValue, $error[0]['property']);
75+
76+
}
77+
1478
public function getInvalidTests()
1579
{
1680
return array(

0 commit comments

Comments
 (0)