-
Notifications
You must be signed in to change notification settings - Fork 357
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add option to throw an exception on validation errors
- Loading branch information
Showing
4 changed files
with
72 additions
and
0 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
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; | ||
|
||
} | ||
} |