Skip to content

Commit

Permalink
Merge pull request #276 from steffkes/path-json-pointer
Browse files Browse the repository at this point in the history
use JsonPointer for path handling
  • Loading branch information
bighappyface authored Aug 11, 2016
2 parents 38503c4 + 7851a44 commit 58b6e07
Show file tree
Hide file tree
Showing 18 changed files with 311 additions and 133 deletions.
14 changes: 8 additions & 6 deletions src/JsonSchema/Constraints/CollectionConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

namespace JsonSchema\Constraints;

use JsonSchema\Entity\JsonPointer;

/**
* The CollectionConstraint Constraints, validates an array against a given schema
*
Expand All @@ -20,7 +22,7 @@ class CollectionConstraint extends Constraint
/**
* {@inheritDoc}
*/
public function check($value, $schema = null, $path = null, $i = null)
public function check($value, $schema = null, JsonPointer $path = null, $i = null)
{
// Verify minItems
if (isset($schema->minItems) && count($value) < $schema->minItems) {
Expand Down Expand Up @@ -52,12 +54,12 @@ public function check($value, $schema = null, $path = null, $i = null)
/**
* Validates the items
*
* @param array $value
* @param \stdClass $schema
* @param string $path
* @param string $i
* @param array $value
* @param \stdClass $schema
* @param JsonPointer|null $path
* @param string $i
*/
protected function validateItems($value, $schema = null, $path = null, $i = null)
protected function validateItems($value, $schema = null, JsonPointer $path = null, $i = null)
{
if (is_object($schema->items)) {
// just one type definition for the whole array
Expand Down
130 changes: 75 additions & 55 deletions src/JsonSchema/Constraints/Constraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use JsonSchema\Uri\UriRetriever;
use JsonSchema\Validator;
use JsonSchema\Entity\JsonPointer;

/**
* The Base Constraints, all Validators should extend this class
Expand Down Expand Up @@ -80,10 +81,11 @@ public function setUriRetriever(UriRetriever $uriRetriever)
/**
* {@inheritDoc}
*/
public function addError($path, $message, $constraint='', array $more=null)
public function addError(JsonPointer $path = null, $message, $constraint='', array $more=null)
{
$error = array(
'property' => $path,
'property' => $this->convertJsonPointerIntoPropertyPath($path ?: new JsonPointer('')),
'pointer' => ltrim(strval($path ?: new JsonPointer('')), '#'),
'message' => $message,
'constraint' => $constraint,
);
Expand Down Expand Up @@ -132,37 +134,32 @@ public function reset()
/**
* Bubble down the path
*
* @param string $path Current path
* @param mixed $i What to append to the path
* @param JsonPointer|null $path Current path
* @param mixed $i What to append to the path
*
* @return string
* @return JsonPointer;
*/
protected function incrementPath($path, $i)
protected function incrementPath(JsonPointer $path = null, $i)
{
if ($path !== '') {
if (is_int($i)) {
$path .= '[' . $i . ']';
} elseif ($i == '') {
$path .= '';
} else {
$path .= '.' . $i;
}
} else {
$path = $i;
}

$path = $path ?: new JsonPointer('');
$path = $path->withPropertyPaths(
array_merge(
$path->getPropertyPaths(),
array_filter(array($i), 'strlen')
)
);
return $path;
}

/**
* Validates an array
*
* @param mixed $value
* @param mixed $schema
* @param mixed $path
* @param mixed $i
* @param mixed $value
* @param mixed $schema
* @param JsonPointer|null $path
* @param mixed $i
*/
protected function checkArray($value, $schema = null, $path = null, $i = null)
protected function checkArray($value, $schema = null, JsonPointer $path = null, $i = null)
{
$validator = $this->getFactory()->createInstanceFor('collection');
$validator->check($value, $schema, $path, $i);
Expand All @@ -173,13 +170,13 @@ protected function checkArray($value, $schema = null, $path = null, $i = null)
/**
* Validates an object
*
* @param mixed $value
* @param mixed $schema
* @param mixed $path
* @param mixed $i
* @param mixed $patternProperties
* @param mixed $value
* @param mixed $schema
* @param JsonPointer|null $path
* @param mixed $i
* @param mixed $patternProperties
*/
protected function checkObject($value, $schema = null, $path = null, $i = null, $patternProperties = null)
protected function checkObject($value, $schema = null, JsonPointer $path = null, $i = null, $patternProperties = null)
{
$validator = $this->getFactory()->createInstanceFor('object');
$validator->check($value, $schema, $path, $i, $patternProperties);
Expand All @@ -190,12 +187,12 @@ protected function checkObject($value, $schema = null, $path = null, $i = null,
/**
* Validates the type of a property
*
* @param mixed $value
* @param mixed $schema
* @param mixed $path
* @param mixed $i
* @param mixed $value
* @param mixed $schema
* @param JsonPointer|null $path
* @param mixed $i
*/
protected function checkType($value, $schema = null, $path = null, $i = null)
protected function checkType($value, $schema = null, JsonPointer $path = null, $i = null)
{
$validator = $this->getFactory()->createInstanceFor('type');
$validator->check($value, $schema, $path, $i);
Expand All @@ -206,12 +203,12 @@ protected function checkType($value, $schema = null, $path = null, $i = null)
/**
* Checks a undefined element
*
* @param mixed $value
* @param mixed $schema
* @param mixed $path
* @param mixed $i
* @param mixed $value
* @param mixed $schema
* @param JsonPointer|null $path
* @param mixed $i
*/
protected function checkUndefined($value, $schema = null, $path = null, $i = null)
protected function checkUndefined($value, $schema = null, JsonPointer $path = null, $i = null)
{
$validator = $this->getFactory()->createInstanceFor('undefined');
$validator->check($value, $schema, $path, $i);
Expand All @@ -222,12 +219,12 @@ protected function checkUndefined($value, $schema = null, $path = null, $i = nul
/**
* Checks a string element
*
* @param mixed $value
* @param mixed $schema
* @param mixed $path
* @param mixed $i
* @param mixed $value
* @param mixed $schema
* @param JsonPointer|null $path
* @param mixed $i
*/
protected function checkString($value, $schema = null, $path = null, $i = null)
protected function checkString($value, $schema = null, JsonPointer $path = null, $i = null)
{
$validator = $this->getFactory()->createInstanceFor('string');
$validator->check($value, $schema, $path, $i);
Expand All @@ -238,12 +235,12 @@ protected function checkString($value, $schema = null, $path = null, $i = null)
/**
* Checks a number element
*
* @param mixed $value
* @param mixed $schema
* @param mixed $path
* @param mixed $i
* @param mixed $value
* @param mixed $schema
* @param JsonPointer $path
* @param mixed $i
*/
protected function checkNumber($value, $schema = null, $path = null, $i = null)
protected function checkNumber($value, $schema = null, JsonPointer $path = null, $i = null)
{
$validator = $this->getFactory()->createInstanceFor('number');
$validator->check($value, $schema, $path, $i);
Expand All @@ -254,20 +251,28 @@ protected function checkNumber($value, $schema = null, $path = null, $i = null)
/**
* Checks a enum element
*
* @param mixed $value
* @param mixed $schema
* @param mixed $path
* @param mixed $i
* @param mixed $value
* @param mixed $schema
* @param JsonPointer|null $path
* @param mixed $i
*/
protected function checkEnum($value, $schema = null, $path = null, $i = null)
protected function checkEnum($value, $schema = null, JsonPointer $path = null, $i = null)
{
$validator = $this->getFactory()->createInstanceFor('enum');
$validator->check($value, $schema, $path, $i);

$this->addErrors($validator->getErrors());
}

protected function checkFormat($value, $schema = null, $path = null, $i = null)
/**
* Checks format of an element
*
* @param mixed $value
* @param mixed $schema
* @param JsonPointer|null $path
* @param mixed $i
*/
protected function checkFormat($value, $schema = null, JsonPointer $path = null, $i = null)
{
$validator = $this->getFactory()->createInstanceFor('format');
$validator->check($value, $schema, $path, $i);
Expand Down Expand Up @@ -298,4 +303,19 @@ protected function getTypeCheck()
{
return $this->getFactory()->getTypeCheck();
}

/**
* @param JsonPointer $pointer
* @return string property path
*/
protected function convertJsonPointerIntoPropertyPath(JsonPointer $pointer)
{
$result = array_map(
function($path) {
return sprintf(is_numeric($path) ? '[%d]' : '.%s', $path);
},
$pointer->getPropertyPaths()
);
return trim(implode('', $result), '.');
}
}
22 changes: 12 additions & 10 deletions src/JsonSchema/Constraints/ConstraintInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

namespace JsonSchema\Constraints;

use JsonSchema\Entity\JsonPointer;

/**
* The Constraints Interface
*
Expand All @@ -33,12 +35,12 @@ public function addErrors(array $errors);
/**
* adds an error
*
* @param string $path
* @param string $message
* @param string $constraint the constraint/rule that is broken, e.g.: 'minLength'
* @param array $more more array elements to add to the error
* @param JsonPointer|null $path
* @param string $message
* @param string $constraint the constraint/rule that is broken, e.g.: 'minLength'
* @param array $more more array elements to add to the error
*/
public function addError($path, $message, $constraint='', array $more=null);
public function addError(JsonPointer $path = null, $message, $constraint='', array $more=null);

/**
* checks if the validator has not raised errors
Expand All @@ -51,11 +53,11 @@ public function isValid();
* invokes the validation of an element
*
* @abstract
* @param mixed $value
* @param mixed $schema
* @param mixed $path
* @param mixed $i
* @param mixed $value
* @param mixed $schema
* @param JsonPointer|null $path
* @param mixed $i
* @throws \JsonSchema\Exception\ExceptionInterface
*/
public function check($value, $schema = null, $path = null, $i = null);
public function check($value, $schema = null, JsonPointer $path = null, $i = null);
}
3 changes: 2 additions & 1 deletion src/JsonSchema/Constraints/EnumConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace JsonSchema\Constraints;
use JsonSchema\Validator;
use JsonSchema\Entity\JsonPointer;

/**
* The EnumConstraint Constraints, validates an element against a given set of possibilities
Expand All @@ -21,7 +22,7 @@ class EnumConstraint extends Constraint
/**
* {@inheritDoc}
*/
public function check($element, $schema = null, $path = null, $i = null)
public function check($element, $schema = null, JsonPointer $path = null, $i = null)
{
// Only validate enum if the attribute exists
if ($element instanceof UndefinedConstraint && (!isset($schema->required) || !$schema->required)) {
Expand Down
4 changes: 3 additions & 1 deletion src/JsonSchema/Constraints/FormatConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
*/

namespace JsonSchema\Constraints;

use JsonSchema\Rfc3339;
use JsonSchema\Entity\JsonPointer;

/**
* Validates against the "format" property
Expand All @@ -21,7 +23,7 @@ class FormatConstraint extends Constraint
/**
* {@inheritDoc}
*/
public function check($element, $schema = null, $path = null, $i = null)
public function check($element, $schema = null, JsonPointer $path = null, $i = null)
{
if (!isset($schema->format)) {
return;
Expand Down
4 changes: 3 additions & 1 deletion src/JsonSchema/Constraints/NumberConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

namespace JsonSchema\Constraints;

use JsonSchema\Entity\JsonPointer;

/**
* The NumberConstraint Constraints, validates an number against a given schema
*
Expand All @@ -20,7 +22,7 @@ class NumberConstraint extends Constraint
/**
* {@inheritDoc}
*/
public function check($element, $schema = null, $path = null, $i = null)
public function check($element, $schema = null, JsonPointer $path = null, $i = null)
{
// Verify minimum
if (isset($schema->exclusiveMinimum)) {
Expand Down
Loading

0 comments on commit 58b6e07

Please sign in to comment.