From 04070260f5e7f3dca740a007ebe1397a30a0fd91 Mon Sep 17 00:00:00 2001 From: Erayd Date: Mon, 13 Mar 2017 09:31:17 +1300 Subject: [PATCH] Add option to disable validation of "format" constraint --- README.md | 1 + src/JsonSchema/Constraints/Constraint.php | 1 + src/JsonSchema/Constraints/FormatConstraint.php | 2 +- tests/Constraints/FormatTest.php | 17 +++++++++++++++++ 4 files changed, 20 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 376c2e27..4de425f5 100644 --- a/README.md +++ b/README.md @@ -188,6 +188,7 @@ third argument to `Validator::validate()`, or can be provided as the third argum | `Constraint::CHECK_MODE_COERCE_TYPES` | Convert data types to match the schema where possible | | `Constraint::CHECK_MODE_APPLY_DEFAULTS` | Apply default values from the schema if not set | | `Constraint::CHECK_MODE_EXCEPTIONS` | Throw an exception immediately if validation fails | +| `Constraint::CHECK_MODE_DISABLE_FORMAT` | Do not validate "format" constraints | Please note that using `Constraint::CHECK_MODE_COERCE_TYPES` or `Constraint::CHECK_MODE_APPLY_DEFAULTS` will modify your original data. diff --git a/src/JsonSchema/Constraints/Constraint.php b/src/JsonSchema/Constraints/Constraint.php index 7fa0a99a..2283ac12 100644 --- a/src/JsonSchema/Constraints/Constraint.php +++ b/src/JsonSchema/Constraints/Constraint.php @@ -30,6 +30,7 @@ abstract class Constraint extends BaseConstraint implements ConstraintInterface const CHECK_MODE_COERCE_TYPES = 0x00000004; const CHECK_MODE_APPLY_DEFAULTS = 0x00000008; const CHECK_MODE_EXCEPTIONS = 0x00000010; + const CHECK_MODE_DISABLE_FORMAT = 0x00000020; /** * Bubble down the path diff --git a/src/JsonSchema/Constraints/FormatConstraint.php b/src/JsonSchema/Constraints/FormatConstraint.php index 5fbd2c40..87c1dc57 100644 --- a/src/JsonSchema/Constraints/FormatConstraint.php +++ b/src/JsonSchema/Constraints/FormatConstraint.php @@ -27,7 +27,7 @@ class FormatConstraint extends Constraint */ public function check(&$element, $schema = null, JsonPointer $path = null, $i = null) { - if (!isset($schema->format)) { + if (!isset($schema->format) || $this->factory->getConfig(self::CHECK_MODE_DISABLE_FORMAT)) { return; } diff --git a/tests/Constraints/FormatTest.php b/tests/Constraints/FormatTest.php index 73de5784..5cca9c01 100644 --- a/tests/Constraints/FormatTest.php +++ b/tests/Constraints/FormatTest.php @@ -9,6 +9,8 @@ namespace JsonSchema\Tests\Constraints; +use JsonSchema\Constraints\Constraint; +use JsonSchema\Constraints\Factory; use JsonSchema\Constraints\FormatConstraint; class FormatTest extends BaseTestCase @@ -76,6 +78,21 @@ public function testInvalidFormat($string, $format) $this->assertEquals(1, count($validator->getErrors()), 'Expected 1 error'); } + /** + * @dataProvider getInvalidFormats + */ + public function testDisabledFormat($string, $format) + { + $factory = new Factory(); + $validator = new FormatConstraint($factory); + $schema = new \stdClass(); + $schema->format = $format; + $factory->addConfig(Constraint::CHECK_MODE_DISABLE_FORMAT); + + $validator->check($string, $schema); + $this->assertEmpty($validator->getErrors()); + } + public function getValidFormats() { return array(