Skip to content

Commit 56fe099

Browse files
committed
Merge pull request #53 from alexmmm/fix-required-validation
Fix draft 3 'required' validation issues
2 parents c99eb30 + 08b9971 commit 56fe099

File tree

2 files changed

+97
-9
lines changed

2 files changed

+97
-9
lines changed

src/JsonSchema/Constraints/Undefined.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,22 +109,23 @@ protected function validateCommonProperties($value, $schema = null, $path = null
109109

110110
// Verify required values
111111
if (is_object($value)) {
112-
if ($value instanceof Undefined) {
113-
// Draft 3 - Required attribute - e.g. "foo": {"type": "string", "required": true}
114-
if (isset($schema->required) && $schema->required) {
115-
$this->addError($path, "is missing and it is required");
116-
}
117-
} else if (isset($schema->required)) {
112+
if (isset($schema->required) && is_array($schema->required) ) {
118113
// Draft 4 - Required is an array of strings - e.g. "required": ["foo", ...]
119114
foreach ($schema->required as $required) {
120115
if (!property_exists($value, $required)) {
121116
$this->addError($path, "the property " . $required . " is required");
122117
}
123118
}
124-
} else {
125-
$this->checkType($value, $schema, $path);
119+
} else if (isset($schema->required)) {
120+
// Draft 3 - Required attribute - e.g. "foo": {"type": "string", "required": true}
121+
if ( $schema->required && $value instanceof Undefined) {
122+
$this->addError($path, "is missing and it is required");
123+
}
126124
}
127-
} else {
125+
}
126+
127+
// Verify type
128+
if (!($value instanceof Undefined)) {
128129
$this->checkType($value, $schema, $path);
129130
}
130131

tests/JsonSchema/Tests/Constraints/RequiredPropertyTest.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,82 @@ public function getInvalidTests()
6868
'{
6969
"required": ["foo"]
7070
}'
71+
),
72+
array(
73+
'{
74+
}',
75+
'{
76+
"type": "object",
77+
"properties": {
78+
"foo": { "required": true }
79+
}
80+
}'
81+
),
82+
array(
83+
'{
84+
"string":{}
85+
}',
86+
'{
87+
"type":"object",
88+
"properties": {
89+
"string":{"type":"string", "required": true}
90+
}
91+
}'
92+
),
93+
array(
94+
'{
95+
"number":{}
96+
}',
97+
'{
98+
"type":"object",
99+
"properties": {
100+
"number":{"type":"number", "required": true}
101+
}
102+
}'
103+
),
104+
array(
105+
'{
106+
"integer":{}
107+
}',
108+
'{
109+
"type":"object",
110+
"properties": {
111+
"integer":{"type":"integer", "required": true}
112+
}
113+
}'
114+
),
115+
array(
116+
'{
117+
"boolean":{}
118+
}',
119+
'{
120+
"type":"object",
121+
"properties": {
122+
"boolean":{"type":"boolean", "required": true}
123+
}
124+
}'
125+
),
126+
array(
127+
'{
128+
"array":{}
129+
}',
130+
'{
131+
"type":"object",
132+
"properties": {
133+
"array":{"type":"array", "required": true}
134+
}
135+
}'
136+
),
137+
array(
138+
'{
139+
"null":{}
140+
}',
141+
'{
142+
"type":"object",
143+
"properties": {
144+
"null":{"type":"null", "required": true}
145+
}
146+
}'
71147
)
72148
);
73149
}
@@ -179,6 +255,17 @@ public function getValidTests()
179255
},
180256
"required": ["foo"]
181257
}'
258+
),
259+
array(
260+
'{
261+
"foo": {}
262+
}',
263+
'{
264+
"type": "object",
265+
"properties": {
266+
"foo": { "required": true }
267+
}
268+
}'
182269
)
183270
);
184271
}

0 commit comments

Comments
 (0)