Skip to content

Commit 745cd08

Browse files
author
samkeen
committed
added tests to ensure the "property" is populated regardless
of required attribute is completely missing or has empty value.
1 parent afce04e commit 745cd08

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

tests/JsonSchema/Tests/Constraints/RequiredPropertyTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,52 @@
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+
$this->assertEquals('foo', $error[0]['property']);
35+
}
36+
37+
public function testErrorPropertyIsPopulatedForRequiredIfEmptyValueInInput()
38+
{
39+
$validator = new UndefinedConstraint();
40+
$document = json_decode('{
41+
"bar": 42,
42+
"foo": null
43+
}');
44+
$schema = json_decode('{
45+
"type": "object",
46+
"properties": {
47+
"foo": {"type": "number"},
48+
"bar": {"type": "number"}
49+
},
50+
"required": ["foo"]
51+
}');
52+
53+
$validator->check($document, $schema);
54+
$error = $validator->getErrors();
55+
$this->assertEquals('foo', $error[0]['property']);
56+
}
57+
1458
public function getInvalidTests()
1559
{
1660
return array(

0 commit comments

Comments
 (0)