-
Notifications
You must be signed in to change notification settings - Fork 356
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Option to throw exceptions when an error is encountered (#354)
* Add option to throw an exception on validation errors * Update README * Changes API references to use the new Validator::validate() entry point * Adds section describing available config options
- Loading branch information
1 parent
9b6ebfe
commit 7eb38e2
Showing
5 changed files
with
106 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} | ||
} |