diff --git a/src/JsonSchema/Constraints/CollectionConstraint.php b/src/JsonSchema/Constraints/CollectionConstraint.php index 7de3bc4d..43e7dd0e 100644 --- a/src/JsonSchema/Constraints/CollectionConstraint.php +++ b/src/JsonSchema/Constraints/CollectionConstraint.php @@ -24,12 +24,12 @@ public function check($value, $schema = null, $path = null, $i = null) { // Verify minItems if (isset($schema->minItems) && count($value) < $schema->minItems) { - $this->addError($path, "There must be a minimum of " . $schema->minItems . " in the array"); + $this->addError($path, "There must be a minimum of " . $schema->minItems . " in the array", 'minItems', array('minItems' => $schema->minItems,)); } // Verify maxItems if (isset($schema->maxItems) && count($value) > $schema->maxItems) { - $this->addError($path, "There must be a maximum of " . $schema->maxItems . " in the array"); + $this->addError($path, "There must be a maximum of " . $schema->maxItems . " in the array", 'maxItems', array('maxItems' => $schema->maxItems,)); } // Verify uniqueItems @@ -39,7 +39,7 @@ public function check($value, $schema = null, $path = null, $i = null) $unique = array_map(function($e) { return var_export($e, true); }, $value); } if (count(array_unique($unique)) != count($value)) { - $this->addError($path, "There are no duplicates allowed in the array"); + $this->addError($path, "There are no duplicates allowed in the array", 'uniqueItems', array('uniqueItems' => $schema->uniqueItems,)); } } @@ -92,7 +92,7 @@ protected function validateItems($value, $schema = null, $path = null, $i = null $this->checkUndefined($v, $schema->additionalItems, $path, $k); } else { $this->addError( - $path, 'The item ' . $i . '[' . $k . '] is not defined and the definition does not allow additional items'); + $path, 'The item ' . $i . '[' . $k . '] is not defined and the definition does not allow additional items', 'additionalItems', array('additionalItems' => $schema->additionalItems,)); } } else { // Should be valid against an empty schema diff --git a/src/JsonSchema/Constraints/Constraint.php b/src/JsonSchema/Constraints/Constraint.php index b7781bc9..d10e9753 100644 --- a/src/JsonSchema/Constraints/Constraint.php +++ b/src/JsonSchema/Constraints/Constraint.php @@ -61,12 +61,20 @@ public function setUriRetriever(UriRetriever $uriRetriever) /** * {@inheritDoc} */ - public function addError($path, $message) + public function addError($path, $message, $constraint='', $more=null) { - $this->errors[] = array( + $a = array( 'property' => $path, - 'message' => $message + 'message' => $message, + 'constraint' => $constraint, ); + + if (is_array($more) && count($more) > 0) + { + $a += $more; + } + + $this->errors[] = $a; } /** diff --git a/src/JsonSchema/Constraints/ConstraintInterface.php b/src/JsonSchema/Constraints/ConstraintInterface.php index 7f65c8e9..32fbf321 100644 --- a/src/JsonSchema/Constraints/ConstraintInterface.php +++ b/src/JsonSchema/Constraints/ConstraintInterface.php @@ -33,10 +33,12 @@ public function addErrors(array $errors); /** * adds an error * - * @param $path - * @param $message + * @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 */ - public function addError($path, $message); + public function addError($path, $message, $constraint='', $more=null); /** * checks if the validator has not raised errors diff --git a/src/JsonSchema/Constraints/EnumConstraint.php b/src/JsonSchema/Constraints/EnumConstraint.php index d0acbf95..186c94df 100644 --- a/src/JsonSchema/Constraints/EnumConstraint.php +++ b/src/JsonSchema/Constraints/EnumConstraint.php @@ -41,6 +41,6 @@ public function check($element, $schema = null, $path = null, $i = null) } } - $this->addError($path, "does not have a value in the enumeration " . print_r($schema->enum, true)); + $this->addError($path, "does not have a value in the enumeration " . print_r($schema->enum, true), 'enum', array('enum' => $schema->enum,)); } } diff --git a/src/JsonSchema/Constraints/FormatConstraint.php b/src/JsonSchema/Constraints/FormatConstraint.php index d939f962..c00baad6 100644 --- a/src/JsonSchema/Constraints/FormatConstraint.php +++ b/src/JsonSchema/Constraints/FormatConstraint.php @@ -29,13 +29,13 @@ public function check($element, $schema = null, $path = null, $i = null) switch ($schema->format) { case 'date': if (!$date = $this->validateDateTime($element, 'Y-m-d')) { - $this->addError($path, sprintf('Invalid date %s, expected format YYYY-MM-DD', json_encode($element))); + $this->addError($path, sprintf('Invalid date %s, expected format YYYY-MM-DD', json_encode($element)), 'format', array('format' => $schema->format,)); } break; case 'time': if (!$this->validateDateTime($element, 'H:i:s')) { - $this->addError($path, sprintf('Invalid time %s, expected format hh:mm:ss', json_encode($element))); + $this->addError($path, sprintf('Invalid time %s, expected format hh:mm:ss', json_encode($element)), 'format', array('format' => $schema->format,)); } break; @@ -45,74 +45,79 @@ public function check($element, $schema = null, $path = null, $i = null) !$this->validateDateTime($element, 'Y-m-d\TH:i:sP') && !$this->validateDateTime($element, 'Y-m-d\TH:i:sO') ) { - $this->addError($path, sprintf('Invalid date-time %s, expected format YYYY-MM-DDThh:mm:ssZ or YYYY-MM-DDThh:mm:ss+hh:mm', json_encode($element))); + $this->addError($path, sprintf('Invalid date-time %s, expected format YYYY-MM-DDThh:mm:ssZ or YYYY-MM-DDThh:mm:ss+hh:mm', json_encode($element)), 'format', array('format' => $schema->format,)); } break; case 'utc-millisec': if (!$this->validateDateTime($element, 'U')) { - $this->addError($path, sprintf('Invalid time %s, expected integer of milliseconds since Epoch', json_encode($element))); + $this->addError($path, sprintf('Invalid time %s, expected integer of milliseconds since Epoch', json_encode($element)), 'format', array('format' => $schema->format,)); } break; case 'regex': if (!$this->validateRegex($element)) { - $this->addError($path, 'Invalid regex format ' . $element); + $this->addError($path, 'Invalid regex format ' . $element, 'format', array('format' => $schema->format,)); } break; case 'color': if (!$this->validateColor($element)) { - $this->addError($path, "Invalid color"); + $this->addError($path, "Invalid color", 'format', array('format' => $schema->format,)); } break; case 'style': if (!$this->validateStyle($element)) { - $this->addError($path, "Invalid style"); + $this->addError($path, "Invalid style", 'format', array('format' => $schema->format,)); } break; case 'phone': if (!$this->validatePhone($element)) { - $this->addError($path, "Invalid phone number"); + $this->addError($path, "Invalid phone number", 'format', array('format' => $schema->format,)); } break; case 'uri': if (null === filter_var($element, FILTER_VALIDATE_URL, FILTER_NULL_ON_FAILURE)) { - $this->addError($path, "Invalid URL format"); + $this->addError($path, "Invalid URL format", 'format', array('format' => $schema->format,)); } break; case 'email': if (null === filter_var($element, FILTER_VALIDATE_EMAIL, FILTER_NULL_ON_FAILURE)) { - $this->addError($path, "Invalid email"); + $this->addError($path, "Invalid email", 'format', array('format' => $schema->format,)); } break; case 'ip-address': case 'ipv4': if (null === filter_var($element, FILTER_VALIDATE_IP, FILTER_NULL_ON_FAILURE | FILTER_FLAG_IPV4)) { - $this->addError($path, "Invalid IP address"); + $this->addError($path, "Invalid IP address", 'format', array('format' => $schema->format,)); } break; case 'ipv6': if (null === filter_var($element, FILTER_VALIDATE_IP, FILTER_NULL_ON_FAILURE | FILTER_FLAG_IPV6)) { - $this->addError($path, "Invalid IP address"); + $this->addError($path, "Invalid IP address", 'format', array('format' => $schema->format,)); } break; case 'host-name': case 'hostname': if (!$this->validateHostname($element)) { - $this->addError($path, "Invalid hostname"); + $this->addError($path, "Invalid hostname", 'format', array('format' => $schema->format,)); } break; default: - $this->addError($path, "Unknown format: " . json_encode($schema->format)); + // Empty as it should be: + // The value of this keyword is called a format attribute. It MUST be a string. + // A format attribute can generally only validate a given set of instance types. + // If the type of the instance to validate is not in this set, validation for + // this format attribute and instance SHOULD succeed. + // http://json-schema.org/latest/json-schema-validation.html#anchor105 break; } } diff --git a/src/JsonSchema/Constraints/NumberConstraint.php b/src/JsonSchema/Constraints/NumberConstraint.php index 49bfbbba..7febb38c 100644 --- a/src/JsonSchema/Constraints/NumberConstraint.php +++ b/src/JsonSchema/Constraints/NumberConstraint.php @@ -26,40 +26,40 @@ public function check($element, $schema = null, $path = null, $i = null) if (isset($schema->exclusiveMinimum)) { if (isset($schema->minimum)) { if ($schema->exclusiveMinimum && $element === $schema->minimum) { - $this->addError($path, "must have a minimum value greater than boundary value of " . $schema->minimum); + $this->addError($path, "must have a minimum value greater than boundary value of " . $schema->minimum, 'exclusiveMinimum', array('minimum' => $schema->minimum,)); } else if ($element < $schema->minimum) { - $this->addError($path, "must have a minimum value of " . $schema->minimum); + $this->addError($path, "must have a minimum value of " . $schema->minimum, 'minimum', array('minimum' => $schema->minimum,)); } } else { - $this->addError($path, "use of exclusiveMinimum requires presence of minimum"); + $this->addError($path, "use of exclusiveMinimum requires presence of minimum", 'missingMinimum'); } } else if (isset($schema->minimum) && $element < $schema->minimum) { - $this->addError($path, "must have a minimum value of " . $schema->minimum); + $this->addError($path, "must have a minimum value of " . $schema->minimum, 'minimum', array('minimum' => $schema->minimum,)); } // Verify maximum if (isset($schema->exclusiveMaximum)) { if (isset($schema->maximum)) { if ($schema->exclusiveMaximum && $element === $schema->maximum) { - $this->addError($path, "must have a maximum value less than boundary value of " . $schema->maximum); + $this->addError($path, "must have a maximum value less than boundary value of " . $schema->maximum, 'exclusiveMaximum', array('maximum' => $schema->maximum,)); } else if ($element > $schema->maximum) { - $this->addError($path, "must have a maximum value of " . $schema->maximum); + $this->addError($path, "must have a maximum value of " . $schema->maximum, 'maximum', array('maximum' => $schema->maximum,)); } } else { - $this->addError($path, "use of exclusiveMaximum requires presence of maximum"); + $this->addError($path, "use of exclusiveMaximum requires presence of maximum", 'missingMinimum'); } } else if (isset($schema->maximum) && $element > $schema->maximum) { - $this->addError($path, "must have a maximum value of " . $schema->maximum); + $this->addError($path, "must have a maximum value of " . $schema->maximum, 'maximum', array('maximum' => $schema->maximum,)); } // Verify divisibleBy - Draft v3 if (isset($schema->divisibleBy) && $this->fmod($element, $schema->divisibleBy) != 0) { - $this->addError($path, "is not divisible by " . $schema->divisibleBy); + $this->addError($path, "is not divisible by " . $schema->divisibleBy, 'divisibleBy', array('divisibleBy' => $schema->divisibleBy,)); } // Verify multipleOf - Draft v4 if (isset($schema->multipleOf) && $this->fmod($element, $schema->multipleOf) != 0) { - $this->addError($path, "must be a multiple of " . $schema->multipleOf); + $this->addError($path, "must be a multiple of " . $schema->multipleOf, 'multipleOf', array('multipleOf' => $schema->multipleOf,)); } $this->checkFormat($element, $schema, $path, $i); diff --git a/src/JsonSchema/Constraints/ObjectConstraint.php b/src/JsonSchema/Constraints/ObjectConstraint.php index 9dc0b188..a93f6eaa 100644 --- a/src/JsonSchema/Constraints/ObjectConstraint.php +++ b/src/JsonSchema/Constraints/ObjectConstraint.php @@ -46,7 +46,7 @@ public function validatePatternProperties($element, $path, $patternProperties) foreach ($patternProperties as $pregex => $schema) { // Validate the pattern before using it to test for matches if (@preg_match('/'. $pregex . '/', '') === false) { - $this->addError($path, 'The pattern "' . $pregex . '" is invalid'); + $this->addError($path, 'The pattern "' . $pregex . '" is invalid', 'pregex', array('pregex' => $pregex,)); continue; } foreach ($element as $i => $value) { @@ -77,7 +77,7 @@ public function validateElement($element, $matches, $objectDefinition = null, $p // no additional properties allowed if (!in_array($i, $matches) && $additionalProp === false && $this->inlineSchemaProperty !== $i && !$definition) { - $this->addError($path, "The property " . $i . " is not defined and the definition does not allow additional properties"); + $this->addError($path, "The property " . $i . " is not defined and the definition does not allow additional properties", 'additionalProp'); } // additional properties defined @@ -92,7 +92,7 @@ public function validateElement($element, $matches, $objectDefinition = null, $p // property requires presence of another $require = $this->getProperty($definition, 'requires'); if ($require && !$this->getProperty($element, $require)) { - $this->addError($path, "the presence of the property " . $i . " requires that " . $require . " also be present"); + $this->addError($path, "the presence of the property " . $i . " requires that " . $require . " also be present", 'requires'); } if (!$definition) { diff --git a/src/JsonSchema/Constraints/StringConstraint.php b/src/JsonSchema/Constraints/StringConstraint.php index a4cd3745..78fd7fcf 100644 --- a/src/JsonSchema/Constraints/StringConstraint.php +++ b/src/JsonSchema/Constraints/StringConstraint.php @@ -24,17 +24,23 @@ public function check($element, $schema = null, $path = null, $i = null) { // Verify maxLength if (isset($schema->maxLength) && $this->strlen($element) > $schema->maxLength) { - $this->addError($path, "must be at most " . $schema->maxLength . " characters long"); + $this->addError($path, "must be at most " . $schema->maxLength . " characters long", 'maxLength', array( + 'maxLength' => $schema->maxLength, + )); } //verify minLength if (isset($schema->minLength) && $this->strlen($element) < $schema->minLength) { - $this->addError($path, "must be at least " . $schema->minLength . " characters long"); + $this->addError($path, "must be at least " . $schema->minLength . " characters long", 'minLength', array( + 'minLength' => $schema->minLength, + )); } // Verify a regex pattern if (isset($schema->pattern) && !preg_match('#' . str_replace('#', '\\#', $schema->pattern) . '#', $element)) { - $this->addError($path, "does not match the regex pattern " . $schema->pattern); + $this->addError($path, "does not match the regex pattern " . $schema->pattern, 'pattern', array( + 'pattern' => $schema->pattern, + )); } $this->checkFormat($element, $schema, $path, $i); diff --git a/src/JsonSchema/Constraints/TypeConstraint.php b/src/JsonSchema/Constraints/TypeConstraint.php index 3c439d54..bea0ffb3 100644 --- a/src/JsonSchema/Constraints/TypeConstraint.php +++ b/src/JsonSchema/Constraints/TypeConstraint.php @@ -80,7 +80,7 @@ public function check($value = null, $schema = null, $path = null, $i = null) implode(', ', array_filter(self::$wording))) ); } - $this->addError($path, gettype($value) . " value found, but " . self::$wording[$type] . " is required"); + $this->addError($path, gettype($value) . " value found, but " . self::$wording[$type] . " is required", 'type'); } } diff --git a/src/JsonSchema/Constraints/UndefinedConstraint.php b/src/JsonSchema/Constraints/UndefinedConstraint.php index 8527b9db..4980d6c4 100644 --- a/src/JsonSchema/Constraints/UndefinedConstraint.php +++ b/src/JsonSchema/Constraints/UndefinedConstraint.php @@ -122,13 +122,13 @@ protected function validateCommonProperties($value, $schema = null, $path = null // Draft 4 - Required is an array of strings - e.g. "required": ["foo", ...] foreach ($schema->required as $required) { if (!property_exists($value, $required)) { - $this->addError($path, "the property " . $required . " is required"); + $this->addError($path, "the property " . $required . " is required", 'required'); } } } else if (isset($schema->required) && !is_array($schema->required)) { // Draft 3 - Required attribute - e.g. "foo": {"type": "string", "required": true} if ( $schema->required && $value instanceof UndefinedConstraint) { - $this->addError($path, "is missing and it is required"); + $this->addError($path, "is missing and it is required", 'required'); } } } @@ -148,7 +148,7 @@ protected function validateCommonProperties($value, $schema = null, $path = null // if no new errors were raised it must be a disallowed value if (count($this->getErrors()) == count($initErrors)) { - $this->addError($path, "disallowed value was matched"); + $this->addError($path, "disallowed value was matched", 'disallow'); } else { $this->errors = $initErrors; } @@ -160,7 +160,7 @@ protected function validateCommonProperties($value, $schema = null, $path = null // if no new errors were raised then the instance validated against the "not" schema if (count($this->getErrors()) == count($initErrors)) { - $this->addError($path, "matched a schema which it should not"); + $this->addError($path, "matched a schema which it should not", 'not'); } else { $this->errors = $initErrors; } @@ -170,12 +170,12 @@ protected function validateCommonProperties($value, $schema = null, $path = null if (is_object($value)) { if (isset($schema->minProperties)) { if (count(get_object_vars($value)) < $schema->minProperties) { - $this->addError($path, "must contain a minimum of " . $schema->minProperties . " properties"); + $this->addError($path, "must contain a minimum of " . $schema->minProperties . " properties", 'minProperties', array('minProperties' => $schema->minProperties,)); } } if (isset($schema->maxProperties)) { if (count(get_object_vars($value)) > $schema->maxProperties) { - $this->addError($path, "must contain no more than " . $schema->maxProperties . " properties"); + $this->addError($path, "must contain no more than " . $schema->maxProperties . " properties", 'maxProperties', array('maxProperties' => $schema->maxProperties,)); } } } @@ -209,7 +209,7 @@ protected function validateOfProperties($value, $schema, $path, $i = "") $isValid = $isValid && (count($this->getErrors()) == count($initErrors)); } if (!$isValid) { - $this->addError($path, "failed to match all schemas"); + $this->addError($path, "failed to match all schemas", 'allOf'); } } @@ -224,7 +224,7 @@ protected function validateOfProperties($value, $schema, $path, $i = "") } } if (!$isValid) { - $this->addError($path, "failed to match at least one schema"); + $this->addError($path, "failed to match at least one schema", 'anyOf'); } else { $this->errors = $startErrors; } @@ -248,7 +248,8 @@ protected function validateOfProperties($value, $schema, $path, $i = "") $allErrors, array(array( 'property' => $path, - 'message' => "failed to match exactly one schema" + 'message' => "failed to match exactly one schema", + 'constraint' => 'oneOf', ),), $startErrors ) @@ -274,13 +275,13 @@ protected function validateDependencies($value, $dependencies, $path, $i = "") if (is_string($dependency)) { // Draft 3 string is allowed - e.g. "dependencies": {"bar": "foo"} if (!property_exists($value, $dependency)) { - $this->addError($path, "$key depends on $dependency and $dependency is missing"); + $this->addError($path, "$key depends on $dependency and $dependency is missing", 'dependencies'); } } else if (is_array($dependency)) { // Draft 4 must be an array - e.g. "dependencies": {"bar": ["foo"]} foreach ($dependency as $d) { if (!property_exists($value, $d)) { - $this->addError($path, "$key depends on $d and $d is missing"); + $this->addError($path, "$key depends on $d and $d is missing", 'dependencies'); } } } else if (is_object($dependency)) { diff --git a/tests/JsonSchema/Tests/Constraints/AdditionalPropertiesTest.php b/tests/JsonSchema/Tests/Constraints/AdditionalPropertiesTest.php index 9df7ae39..1e615e76 100644 --- a/tests/JsonSchema/Tests/Constraints/AdditionalPropertiesTest.php +++ b/tests/JsonSchema/Tests/Constraints/AdditionalPropertiesTest.php @@ -35,8 +35,9 @@ public function getInvalidTests() null, array( array( - 'property' => '', - 'message' => 'The property additionalProp is not defined and the definition does not allow additional properties' + 'property' => '', + 'message' => 'The property additionalProp is not defined and the definition does not allow additional properties', + 'constraint' => 'additionalProp', ) ) ), diff --git a/tests/JsonSchema/Tests/Constraints/OfPropertiesTest.php b/tests/JsonSchema/Tests/Constraints/OfPropertiesTest.php index b06628d3..b4875c11 100644 --- a/tests/JsonSchema/Tests/Constraints/OfPropertiesTest.php +++ b/tests/JsonSchema/Tests/Constraints/OfPropertiesTest.php @@ -74,17 +74,19 @@ public function getInvalidTests() Validator::CHECK_MODE_NORMAL, array( array( - "property" => "prop2", - "message" => "array value found, but a string is required", - + "property" => "prop2", + "message" => "array value found, but a string is required", + "constraint" => "type", ), array( - "property" => "prop2", - "message" => "array value found, but a number is required", + "property" => "prop2", + "message" => "array value found, but a number is required", + "constraint" => "type", ), array( - "property" => "prop2", - "message" => "failed to match exactly one schema", + "property" => "prop2", + "message" => "failed to match exactly one schema", + "constraint" => "oneOf", ), ), ),