Skip to content

Commit 62aef11

Browse files
author
Michael Chiocca
committed
Fix extends so that it accepts a schema or an array of schemas.
1 parent bf92610 commit 62aef11

File tree

2 files changed

+91
-36
lines changed

2 files changed

+91
-36
lines changed

src/JsonSchema/Constraints/Undefined.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,16 @@ protected function validateCommonProperties($value, $schema = null, $path = null
9090
// if it extends another schema, it must pass that schema as well
9191
if (isset($schema->extends)) {
9292
if (is_string($schema->extends)) {
93-
$schema->extends = $this->validateUri($schema->extends, $schema, $path, $i);
93+
$schema->extends = $this->validateUri($schema, $schema->extends);
94+
}
95+
$increment = is_null($i) ? "" : $i;
96+
if (is_array($schema->extends)) {
97+
foreach ($schema->extends as $extends) {
98+
$this->checkUndefined($value, $extends, $path, $increment);
99+
}
100+
} else {
101+
$this->checkUndefined($value, $schema->extends, $path, $increment);
94102
}
95-
$this->checkUndefined($value, $schema->extends, $path, $i);
96103
}
97104

98105
// Verify required values
@@ -150,26 +157,26 @@ protected function validateDependencies($value, $dependencies, $path)
150157
foreach ($dependencies as $key => $dependency) {
151158
if (property_exists($value, $key)) {
152159
if (is_string($dependency)) {
153-
// Draft 3 string is allowed - e.g. "dependencies": "foo"
160+
// Draft 3 string is allowed - e.g. "dependencies": {"bar": "foo"}
154161
if (!property_exists($value, $dependency)) {
155162
$this->addError($path, "$key depends on $dependency and $dependency is missing");
156163
}
157164
} else if (is_array($dependency)) {
158-
// Draft 4 doesn't allow string so must be an array - e.g. "dependencies": ["foo"]
165+
// Draft 4 must be an array - e.g. "dependencies": {"bar": ["foo"]}
159166
foreach ($dependency as $d) {
160167
if (!property_exists($value, $d)) {
161168
$this->addError($path, "$key depends on $d and $d is missing");
162169
}
163170
}
164171
} else if (is_object($dependency)) {
165-
// The dependency is a schema - e.g. "dependencies": {"properties": {"foo": {...}}}
172+
// Schema - e.g. "dependencies": {"bar": {"properties": {"foo": {...}}}}
166173
$this->checkUndefined($value, $dependency, $path, "");
167174
}
168175
}
169176
}
170177
}
171178

172-
protected function validateUri($schemaUri = null, $schema, $path = null, $i = null)
179+
protected function validateUri($schema, $schemaUri = null)
173180
{
174181
$resolver = new \JsonSchema\Uri\UriResolver();
175182
$retriever = $this->getUriRetriever();

tests/JsonSchema/Tests/Constraints/ExtendsTest.php

Lines changed: 78 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,16 @@ public function getInvalidTests()
2020
"age":50
2121
}',
2222
'{
23-
"id": "person",
24-
"type": "object",
2523
"properties": {
26-
"name": {
27-
"type": "string"
28-
},
29-
"age" : {
24+
"name": {"type": "string"},
25+
"age": {
3026
"type": "integer",
31-
"maximum":120
27+
"maximum": 120
3228
}
3329
},
3430
"extends": {
35-
"id": "oldPerson",
36-
"type": "object",
3731
"properties": {
38-
"age" : {"minimum":70}
32+
"age": {"minimum": 70}
3933
}
4034
}
4135
}'
@@ -46,25 +40,52 @@ public function getInvalidTests()
4640
"age":180
4741
}',
4842
'{
49-
"id": "person",
50-
"type": "object",
5143
"properties": {
52-
"name": {
53-
"type": "string"
54-
},
55-
"age" : {
44+
"name": {"type": "string"},
45+
"age": {
5646
"type": "integer",
57-
"maximum":120
47+
"maximum": 120
48+
}
49+
},
50+
"extends": {
51+
"properties": {
52+
"age": {"minimum":70}
5853
}
54+
}
55+
}'
56+
),
57+
array(
58+
'{"foo": 2, "bar": "baz"}',
59+
'{
60+
"properties": {
61+
"bar": {"type": "integer", "required": true}
5962
},
6063
"extends": {
61-
"id": "oldPerson",
62-
"type": "object",
6364
"properties": {
64-
"age" : {"minimum":70}
65+
"foo": {"type": "string", "required": true}
6566
}
6667
}
6768
}'
69+
),
70+
array(
71+
'{"bar": 2}',
72+
'{
73+
"properties": {
74+
"bar": {"type": "integer", "required": true}
75+
},
76+
"extends" : [
77+
{
78+
"properties": {
79+
"foo": {"type": "string", "required": true}
80+
}
81+
},
82+
{
83+
"properties": {
84+
"baz": {"type": "null", "required": true}
85+
}
86+
}
87+
]
88+
}'
6889
)
6990
);
7091
}
@@ -78,25 +99,52 @@ public function getValidTests()
7899
"age":80
79100
}',
80101
'{
81-
"id": "person",
82-
"type": "object",
83102
"properties": {
84-
"name": {
85-
"type": "string"
86-
},
87-
"age" : {
103+
"name": {"type": "string"},
104+
"age": {
88105
"type": "integer",
89-
"maximum":120
106+
"maximum": 120
90107
}
91108
},
92109
"extends": {
93-
"id": "oldPerson",
94-
"type": "object",
95110
"properties": {
96-
"age" : {"minimum":70}
111+
"age": {"minimum": 70}
97112
}
98113
}
99114
}'
115+
),
116+
array(
117+
'{"foo": "baz", "bar": 2}',
118+
'{
119+
"properties": {
120+
"bar": {"type": "integer", "required": true}
121+
},
122+
"extends": {
123+
"properties": {
124+
"foo": {"type": "string", "required": true}
125+
}
126+
}
127+
}'
128+
),
129+
array(
130+
'{"foo": "ick", "bar": 2, "baz": null}',
131+
'{
132+
"properties": {
133+
"bar": {"type": "integer", "required": true}
134+
},
135+
"extends" : [
136+
{
137+
"properties": {
138+
"foo": {"type": "string", "required": true}
139+
}
140+
},
141+
{
142+
"properties": {
143+
"baz": {"type": "null", "required": true}
144+
}
145+
}
146+
]
147+
}'
100148
)
101149
);
102150
}

0 commit comments

Comments
 (0)