Skip to content

Commit a2f4418

Browse files
committed
Add option to throw an exception on validation errors
1 parent ba93d39 commit a2f4418

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed

src/JsonSchema/Constraints/BaseConstraint.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace JsonSchema\Constraints;
1111

1212
use JsonSchema\Entity\JsonPointer;
13+
use JsonSchema\Exception\ValidationException;
1314

1415
/**
1516
* A more basic constraint definition - used for the public
@@ -47,6 +48,10 @@ public function addError(JsonPointer $path = null, $message, $constraint = '', a
4748
'constraint' => $constraint,
4849
);
4950

51+
if ($this->factory->getConfig(Constraint::CHECK_MODE_EXCEPTIONS)) {
52+
throw new ValidationException(sprintf("Error validating %s: %s", $error['pointer'], $error['message']));
53+
}
54+
5055
if (is_array($more) && count($more) > 0)
5156
{
5257
$error += $more;

src/JsonSchema/Constraints/Constraint.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ abstract class Constraint extends BaseConstraint implements ConstraintInterface
2929
const CHECK_MODE_TYPE_CAST = 0x00000002;
3030
const CHECK_MODE_COERCE_TYPES = 0x00000004;
3131
const CHECK_MODE_APPLY_DEFAULTS = 0x00000008;
32+
const CHECK_MODE_EXCEPTIONS = 0x00000010;
3233

3334
/**
3435
* Bubble down the path
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the JsonSchema package.
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
namespace JsonSchema\Exception;
11+
12+
class ValidationException extends RuntimeException
13+
{
14+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the JsonSchema package.
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
namespace JsonSchema\Tests\Constraints;
11+
12+
use JsonSchema\Validator;
13+
use JsonSchema\Constraints\Constraint;
14+
use JsonSchema\Exception\ValidationException;
15+
16+
class ValidationExceptionTest extends \PHPUnit_Framework_TestCase
17+
{
18+
public function testValidationException()
19+
{
20+
$exception = new ValidationException();
21+
$this->assertInstanceOf('\JsonSchema\Exception\ValidationException', $exception);
22+
23+
$checkValue = json_decode('{"propertyOne": "thisIsNotAnObject"}');
24+
$schema = json_decode('{
25+
"type": "object",
26+
"additionalProperties": false,
27+
"properties": {
28+
"propertyOne": {
29+
"type": "object"
30+
}
31+
}
32+
}');
33+
34+
$validator = new Validator();
35+
36+
try {
37+
$validator->validate($checkValue, $schema, Constraint::CHECK_MODE_EXCEPTIONS);
38+
} catch (\Exception $e) {
39+
$exception = $e;
40+
}
41+
42+
$this->assertEquals(
43+
'Error validating /propertyOne: String value found, but an object is required',
44+
$exception->getMessage()
45+
);
46+
47+
48+
$this->setExpectedException('JsonSchema\Exception\ValidationException');
49+
throw $exception;
50+
51+
}
52+
}

0 commit comments

Comments
 (0)