diff --git a/src/JsonSchema/Constraints/BaseConstraint.php b/src/JsonSchema/Constraints/BaseConstraint.php index 9d2ade65..6c4cc42f 100644 --- a/src/JsonSchema/Constraints/BaseConstraint.php +++ b/src/JsonSchema/Constraints/BaseConstraint.php @@ -10,6 +10,7 @@ namespace JsonSchema\Constraints; use JsonSchema\Entity\JsonPointer; +use JsonSchema\Exception\ValidationException; /** * A more basic constraint definition - used for the public @@ -47,6 +48,10 @@ public function addError(JsonPointer $path = null, $message, $constraint = '', a 'constraint' => $constraint, ); + if ($this->factory->getConfig(Constraint::CHECK_MODE_EXCEPTIONS)) { + throw new ValidationException(sprintf("Error validating %s: %s", $error['pointer'], $error['message'])); + } + if (is_array($more) && count($more) > 0) { $error += $more; diff --git a/src/JsonSchema/Constraints/Constraint.php b/src/JsonSchema/Constraints/Constraint.php index c5170eea..cc12a22c 100644 --- a/src/JsonSchema/Constraints/Constraint.php +++ b/src/JsonSchema/Constraints/Constraint.php @@ -29,6 +29,7 @@ abstract class Constraint extends BaseConstraint implements ConstraintInterface const CHECK_MODE_TYPE_CAST = 0x00000002; const CHECK_MODE_COERCE_TYPES = 0x00000004; const CHECK_MODE_APPLY_DEFAULTS = 0x00000008; + const CHECK_MODE_EXCEPTIONS = 0x00000010; /** * Bubble down the path diff --git a/src/JsonSchema/Exception/ValidationException.php b/src/JsonSchema/Exception/ValidationException.php new file mode 100644 index 00000000..65d8849e --- /dev/null +++ b/src/JsonSchema/Exception/ValidationException.php @@ -0,0 +1,14 @@ +assertInstanceOf('\JsonSchema\Exception\ValidationException', $exception); + + $checkValue = json_decode('{"propertyOne": "thisIsNotAnObject"}'); + $schema = json_decode('{ + "type": "object", + "additionalProperties": false, + "properties": { + "propertyOne": { + "type": "object" + } + } + }'); + + $validator = new Validator(); + + try { + $validator->validate($checkValue, $schema, Constraint::CHECK_MODE_EXCEPTIONS); + } catch (\Exception $e) { + $exception = $e; + } + + $this->assertEquals( + 'Error validating /propertyOne: String value found, but an object is required', + $exception->getMessage() + ); + + + $this->setExpectedException('JsonSchema\Exception\ValidationException'); + throw $exception; + + } +}