Skip to content

Commit b8d2611

Browse files
eraydbighappyface
authored andcommitted
Add option to disable validation of "format" constraint (#383)
1 parent d3f7740 commit b8d2611

File tree

4 files changed

+20
-1
lines changed

4 files changed

+20
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ third argument to `Validator::validate()`, or can be provided as the third argum
188188
| `Constraint::CHECK_MODE_COERCE_TYPES` | Convert data types to match the schema where possible |
189189
| `Constraint::CHECK_MODE_APPLY_DEFAULTS` | Apply default values from the schema if not set |
190190
| `Constraint::CHECK_MODE_EXCEPTIONS` | Throw an exception immediately if validation fails |
191+
| `Constraint::CHECK_MODE_DISABLE_FORMAT` | Do not validate "format" constraints |
191192

192193
Please note that using `Constraint::CHECK_MODE_COERCE_TYPES` or `Constraint::CHECK_MODE_APPLY_DEFAULTS`
193194
will modify your original data.

src/JsonSchema/Constraints/Constraint.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ abstract class Constraint extends BaseConstraint implements ConstraintInterface
3030
const CHECK_MODE_COERCE_TYPES = 0x00000004;
3131
const CHECK_MODE_APPLY_DEFAULTS = 0x00000008;
3232
const CHECK_MODE_EXCEPTIONS = 0x00000010;
33+
const CHECK_MODE_DISABLE_FORMAT = 0x00000020;
3334

3435
/**
3536
* Bubble down the path

src/JsonSchema/Constraints/FormatConstraint.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class FormatConstraint extends Constraint
2727
*/
2828
public function check(&$element, $schema = null, JsonPointer $path = null, $i = null)
2929
{
30-
if (!isset($schema->format)) {
30+
if (!isset($schema->format) || $this->factory->getConfig(self::CHECK_MODE_DISABLE_FORMAT)) {
3131
return;
3232
}
3333

tests/Constraints/FormatTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
namespace JsonSchema\Tests\Constraints;
1111

12+
use JsonSchema\Constraints\Constraint;
13+
use JsonSchema\Constraints\Factory;
1214
use JsonSchema\Constraints\FormatConstraint;
1315

1416
class FormatTest extends BaseTestCase
@@ -76,6 +78,21 @@ public function testInvalidFormat($string, $format)
7678
$this->assertEquals(1, count($validator->getErrors()), 'Expected 1 error');
7779
}
7880

81+
/**
82+
* @dataProvider getInvalidFormats
83+
*/
84+
public function testDisabledFormat($string, $format)
85+
{
86+
$factory = new Factory();
87+
$validator = new FormatConstraint($factory);
88+
$schema = new \stdClass();
89+
$schema->format = $format;
90+
$factory->addConfig(Constraint::CHECK_MODE_DISABLE_FORMAT);
91+
92+
$validator->check($string, $schema);
93+
$this->assertEmpty($validator->getErrors());
94+
}
95+
7996
public function getValidFormats()
8097
{
8198
return array(

0 commit comments

Comments
 (0)