Skip to content

Commit 58b6e07

Browse files
authored
Merge pull request #276 from steffkes/path-json-pointer
use JsonPointer for path handling
2 parents 38503c4 + 7851a44 commit 58b6e07

18 files changed

+311
-133
lines changed

src/JsonSchema/Constraints/CollectionConstraint.php

+8-6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
namespace JsonSchema\Constraints;
1111

12+
use JsonSchema\Entity\JsonPointer;
13+
1214
/**
1315
* The CollectionConstraint Constraints, validates an array against a given schema
1416
*
@@ -20,7 +22,7 @@ class CollectionConstraint extends Constraint
2022
/**
2123
* {@inheritDoc}
2224
*/
23-
public function check($value, $schema = null, $path = null, $i = null)
25+
public function check($value, $schema = null, JsonPointer $path = null, $i = null)
2426
{
2527
// Verify minItems
2628
if (isset($schema->minItems) && count($value) < $schema->minItems) {
@@ -52,12 +54,12 @@ public function check($value, $schema = null, $path = null, $i = null)
5254
/**
5355
* Validates the items
5456
*
55-
* @param array $value
56-
* @param \stdClass $schema
57-
* @param string $path
58-
* @param string $i
57+
* @param array $value
58+
* @param \stdClass $schema
59+
* @param JsonPointer|null $path
60+
* @param string $i
5961
*/
60-
protected function validateItems($value, $schema = null, $path = null, $i = null)
62+
protected function validateItems($value, $schema = null, JsonPointer $path = null, $i = null)
6163
{
6264
if (is_object($schema->items)) {
6365
// just one type definition for the whole array

src/JsonSchema/Constraints/Constraint.php

+75-55
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use JsonSchema\Uri\UriRetriever;
1313
use JsonSchema\Validator;
14+
use JsonSchema\Entity\JsonPointer;
1415

1516
/**
1617
* The Base Constraints, all Validators should extend this class
@@ -80,10 +81,11 @@ public function setUriRetriever(UriRetriever $uriRetriever)
8081
/**
8182
* {@inheritDoc}
8283
*/
83-
public function addError($path, $message, $constraint='', array $more=null)
84+
public function addError(JsonPointer $path = null, $message, $constraint='', array $more=null)
8485
{
8586
$error = array(
86-
'property' => $path,
87+
'property' => $this->convertJsonPointerIntoPropertyPath($path ?: new JsonPointer('')),
88+
'pointer' => ltrim(strval($path ?: new JsonPointer('')), '#'),
8789
'message' => $message,
8890
'constraint' => $constraint,
8991
);
@@ -132,37 +134,32 @@ public function reset()
132134
/**
133135
* Bubble down the path
134136
*
135-
* @param string $path Current path
136-
* @param mixed $i What to append to the path
137+
* @param JsonPointer|null $path Current path
138+
* @param mixed $i What to append to the path
137139
*
138-
* @return string
140+
* @return JsonPointer;
139141
*/
140-
protected function incrementPath($path, $i)
142+
protected function incrementPath(JsonPointer $path = null, $i)
141143
{
142-
if ($path !== '') {
143-
if (is_int($i)) {
144-
$path .= '[' . $i . ']';
145-
} elseif ($i == '') {
146-
$path .= '';
147-
} else {
148-
$path .= '.' . $i;
149-
}
150-
} else {
151-
$path = $i;
152-
}
153-
144+
$path = $path ?: new JsonPointer('');
145+
$path = $path->withPropertyPaths(
146+
array_merge(
147+
$path->getPropertyPaths(),
148+
array_filter(array($i), 'strlen')
149+
)
150+
);
154151
return $path;
155152
}
156153

157154
/**
158155
* Validates an array
159156
*
160-
* @param mixed $value
161-
* @param mixed $schema
162-
* @param mixed $path
163-
* @param mixed $i
157+
* @param mixed $value
158+
* @param mixed $schema
159+
* @param JsonPointer|null $path
160+
* @param mixed $i
164161
*/
165-
protected function checkArray($value, $schema = null, $path = null, $i = null)
162+
protected function checkArray($value, $schema = null, JsonPointer $path = null, $i = null)
166163
{
167164
$validator = $this->getFactory()->createInstanceFor('collection');
168165
$validator->check($value, $schema, $path, $i);
@@ -173,13 +170,13 @@ protected function checkArray($value, $schema = null, $path = null, $i = null)
173170
/**
174171
* Validates an object
175172
*
176-
* @param mixed $value
177-
* @param mixed $schema
178-
* @param mixed $path
179-
* @param mixed $i
180-
* @param mixed $patternProperties
173+
* @param mixed $value
174+
* @param mixed $schema
175+
* @param JsonPointer|null $path
176+
* @param mixed $i
177+
* @param mixed $patternProperties
181178
*/
182-
protected function checkObject($value, $schema = null, $path = null, $i = null, $patternProperties = null)
179+
protected function checkObject($value, $schema = null, JsonPointer $path = null, $i = null, $patternProperties = null)
183180
{
184181
$validator = $this->getFactory()->createInstanceFor('object');
185182
$validator->check($value, $schema, $path, $i, $patternProperties);
@@ -190,12 +187,12 @@ protected function checkObject($value, $schema = null, $path = null, $i = null,
190187
/**
191188
* Validates the type of a property
192189
*
193-
* @param mixed $value
194-
* @param mixed $schema
195-
* @param mixed $path
196-
* @param mixed $i
190+
* @param mixed $value
191+
* @param mixed $schema
192+
* @param JsonPointer|null $path
193+
* @param mixed $i
197194
*/
198-
protected function checkType($value, $schema = null, $path = null, $i = null)
195+
protected function checkType($value, $schema = null, JsonPointer $path = null, $i = null)
199196
{
200197
$validator = $this->getFactory()->createInstanceFor('type');
201198
$validator->check($value, $schema, $path, $i);
@@ -206,12 +203,12 @@ protected function checkType($value, $schema = null, $path = null, $i = null)
206203
/**
207204
* Checks a undefined element
208205
*
209-
* @param mixed $value
210-
* @param mixed $schema
211-
* @param mixed $path
212-
* @param mixed $i
206+
* @param mixed $value
207+
* @param mixed $schema
208+
* @param JsonPointer|null $path
209+
* @param mixed $i
213210
*/
214-
protected function checkUndefined($value, $schema = null, $path = null, $i = null)
211+
protected function checkUndefined($value, $schema = null, JsonPointer $path = null, $i = null)
215212
{
216213
$validator = $this->getFactory()->createInstanceFor('undefined');
217214
$validator->check($value, $schema, $path, $i);
@@ -222,12 +219,12 @@ protected function checkUndefined($value, $schema = null, $path = null, $i = nul
222219
/**
223220
* Checks a string element
224221
*
225-
* @param mixed $value
226-
* @param mixed $schema
227-
* @param mixed $path
228-
* @param mixed $i
222+
* @param mixed $value
223+
* @param mixed $schema
224+
* @param JsonPointer|null $path
225+
* @param mixed $i
229226
*/
230-
protected function checkString($value, $schema = null, $path = null, $i = null)
227+
protected function checkString($value, $schema = null, JsonPointer $path = null, $i = null)
231228
{
232229
$validator = $this->getFactory()->createInstanceFor('string');
233230
$validator->check($value, $schema, $path, $i);
@@ -238,12 +235,12 @@ protected function checkString($value, $schema = null, $path = null, $i = null)
238235
/**
239236
* Checks a number element
240237
*
241-
* @param mixed $value
242-
* @param mixed $schema
243-
* @param mixed $path
244-
* @param mixed $i
238+
* @param mixed $value
239+
* @param mixed $schema
240+
* @param JsonPointer $path
241+
* @param mixed $i
245242
*/
246-
protected function checkNumber($value, $schema = null, $path = null, $i = null)
243+
protected function checkNumber($value, $schema = null, JsonPointer $path = null, $i = null)
247244
{
248245
$validator = $this->getFactory()->createInstanceFor('number');
249246
$validator->check($value, $schema, $path, $i);
@@ -254,20 +251,28 @@ protected function checkNumber($value, $schema = null, $path = null, $i = null)
254251
/**
255252
* Checks a enum element
256253
*
257-
* @param mixed $value
258-
* @param mixed $schema
259-
* @param mixed $path
260-
* @param mixed $i
254+
* @param mixed $value
255+
* @param mixed $schema
256+
* @param JsonPointer|null $path
257+
* @param mixed $i
261258
*/
262-
protected function checkEnum($value, $schema = null, $path = null, $i = null)
259+
protected function checkEnum($value, $schema = null, JsonPointer $path = null, $i = null)
263260
{
264261
$validator = $this->getFactory()->createInstanceFor('enum');
265262
$validator->check($value, $schema, $path, $i);
266263

267264
$this->addErrors($validator->getErrors());
268265
}
269266

270-
protected function checkFormat($value, $schema = null, $path = null, $i = null)
267+
/**
268+
* Checks format of an element
269+
*
270+
* @param mixed $value
271+
* @param mixed $schema
272+
* @param JsonPointer|null $path
273+
* @param mixed $i
274+
*/
275+
protected function checkFormat($value, $schema = null, JsonPointer $path = null, $i = null)
271276
{
272277
$validator = $this->getFactory()->createInstanceFor('format');
273278
$validator->check($value, $schema, $path, $i);
@@ -298,4 +303,19 @@ protected function getTypeCheck()
298303
{
299304
return $this->getFactory()->getTypeCheck();
300305
}
306+
307+
/**
308+
* @param JsonPointer $pointer
309+
* @return string property path
310+
*/
311+
protected function convertJsonPointerIntoPropertyPath(JsonPointer $pointer)
312+
{
313+
$result = array_map(
314+
function($path) {
315+
return sprintf(is_numeric($path) ? '[%d]' : '.%s', $path);
316+
},
317+
$pointer->getPropertyPaths()
318+
);
319+
return trim(implode('', $result), '.');
320+
}
301321
}

src/JsonSchema/Constraints/ConstraintInterface.php

+12-10
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
namespace JsonSchema\Constraints;
1111

12+
use JsonSchema\Entity\JsonPointer;
13+
1214
/**
1315
* The Constraints Interface
1416
*
@@ -33,12 +35,12 @@ public function addErrors(array $errors);
3335
/**
3436
* adds an error
3537
*
36-
* @param string $path
37-
* @param string $message
38-
* @param string $constraint the constraint/rule that is broken, e.g.: 'minLength'
39-
* @param array $more more array elements to add to the error
38+
* @param JsonPointer|null $path
39+
* @param string $message
40+
* @param string $constraint the constraint/rule that is broken, e.g.: 'minLength'
41+
* @param array $more more array elements to add to the error
4042
*/
41-
public function addError($path, $message, $constraint='', array $more=null);
43+
public function addError(JsonPointer $path = null, $message, $constraint='', array $more=null);
4244

4345
/**
4446
* checks if the validator has not raised errors
@@ -51,11 +53,11 @@ public function isValid();
5153
* invokes the validation of an element
5254
*
5355
* @abstract
54-
* @param mixed $value
55-
* @param mixed $schema
56-
* @param mixed $path
57-
* @param mixed $i
56+
* @param mixed $value
57+
* @param mixed $schema
58+
* @param JsonPointer|null $path
59+
* @param mixed $i
5860
* @throws \JsonSchema\Exception\ExceptionInterface
5961
*/
60-
public function check($value, $schema = null, $path = null, $i = null);
62+
public function check($value, $schema = null, JsonPointer $path = null, $i = null);
6163
}

src/JsonSchema/Constraints/EnumConstraint.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace JsonSchema\Constraints;
1111
use JsonSchema\Validator;
12+
use JsonSchema\Entity\JsonPointer;
1213

1314
/**
1415
* The EnumConstraint Constraints, validates an element against a given set of possibilities
@@ -21,7 +22,7 @@ class EnumConstraint extends Constraint
2122
/**
2223
* {@inheritDoc}
2324
*/
24-
public function check($element, $schema = null, $path = null, $i = null)
25+
public function check($element, $schema = null, JsonPointer $path = null, $i = null)
2526
{
2627
// Only validate enum if the attribute exists
2728
if ($element instanceof UndefinedConstraint && (!isset($schema->required) || !$schema->required)) {

src/JsonSchema/Constraints/FormatConstraint.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
*/
99

1010
namespace JsonSchema\Constraints;
11+
1112
use JsonSchema\Rfc3339;
13+
use JsonSchema\Entity\JsonPointer;
1214

1315
/**
1416
* Validates against the "format" property
@@ -21,7 +23,7 @@ class FormatConstraint extends Constraint
2123
/**
2224
* {@inheritDoc}
2325
*/
24-
public function check($element, $schema = null, $path = null, $i = null)
26+
public function check($element, $schema = null, JsonPointer $path = null, $i = null)
2527
{
2628
if (!isset($schema->format)) {
2729
return;

src/JsonSchema/Constraints/NumberConstraint.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
namespace JsonSchema\Constraints;
1111

12+
use JsonSchema\Entity\JsonPointer;
13+
1214
/**
1315
* The NumberConstraint Constraints, validates an number against a given schema
1416
*
@@ -20,7 +22,7 @@ class NumberConstraint extends Constraint
2022
/**
2123
* {@inheritDoc}
2224
*/
23-
public function check($element, $schema = null, $path = null, $i = null)
25+
public function check($element, $schema = null, JsonPointer $path = null, $i = null)
2426
{
2527
// Verify minimum
2628
if (isset($schema->exclusiveMinimum)) {

0 commit comments

Comments
 (0)