Skip to content

Commit

Permalink
Add option to disable validation of "format" constraint (jsonrainbow#383
Browse files Browse the repository at this point in the history
)
  • Loading branch information
erayd committed Mar 17, 2017
1 parent 92c6f4e commit 1d28a68
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions src/JsonSchema/Constraints/Constraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/JsonSchema/Constraints/FormatConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,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;
}

Expand Down
17 changes: 17 additions & 0 deletions tests/Constraints/FormatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

namespace JsonSchema\Tests\Constraints;

use JsonSchema\Constraints\Constraint;
use JsonSchema\Constraints\Factory;
use JsonSchema\Constraints\FormatConstraint;

class FormatTest extends BaseTestCase
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 1d28a68

Please sign in to comment.