Skip to content

Commit

Permalink
Add option to throw an exception on validation errors
Browse files Browse the repository at this point in the history
  • Loading branch information
erayd committed Feb 5, 2017
1 parent 2159893 commit 105e153
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/JsonSchema/Constraints/BaseConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace JsonSchema\Constraints;

use JsonSchema\Entity\JsonPointer;
use JsonSchema\Exception\ValidationException;

/**
* A more basic constraint definition - used for the public
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/JsonSchema/Constraints/Constraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions src/JsonSchema/Exception/ValidationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

/*
* This file is part of the JsonSchema package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace JsonSchema\Exception;

class ValidationException extends RuntimeException
{
}
52 changes: 52 additions & 0 deletions tests/Constraints/ValidationExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

/*
* This file is part of the JsonSchema package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace JsonSchema\Tests\Constraints;

use JsonSchema\Validator;
use JsonSchema\Constraints\Constraint;
use JsonSchema\Exception\ValidationException;

class ValidationExceptionTest extends \PHPUnit_Framework_TestCase
{
public function testValidationException()
{
$exception = new ValidationException();
$this->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;

}
}

0 comments on commit 105e153

Please sign in to comment.