Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,13 +211,16 @@ third argument to `Validator::validate()`, or can be provided as the third argum
| `Constraint::CHECK_MODE_DISABLE_FORMAT` | Do not validate "format" constraints |
| `Constraint::CHECK_MODE_VALIDATE_SCHEMA` | Validate the schema as well as the provided document |
| `Constraint::CHECK_MODE_STRICT` [^3] | Validate the scheme using strict mode using the specified draft |
| `Constraint::CHECK_MODE_FAIL_FAST` [^4] | Stop at the first error, only one error is reported |

[^1]: Please note that using `CHECK_MODE_COERCE_TYPES` or `CHECK_MODE_APPLY_DEFAULTS` will modify your
original data.
[^2]: `CHECK_MODE_EARLY_COERCE` has no effect unless used in combination with `CHECK_MODE_COERCE_TYPES`. If
enabled, the validator will use (and coerce) the first compatible type it encounters, even if the
schema defines another type that matches directly and does not require coercion.
[^3]: `CHECK_MODE_STRICT` only can be used for Draft-6, Draft 7 or Draft 2019-09 at this point.
[^4]: `CHECK_MODE_FAIL_FAST` only has an effect in combination with `CHECK_MODE_STRICT`. Use it when only the validity of
the data is needed, as validation can then skip the rest of the data once an error is found.

## Running the tests

Expand Down
4 changes: 2 additions & 2 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@ parameters:
path: src/JsonSchema/Constraints/Factory.php

-
message: "#^Method JsonSchema\\\\Constraints\\\\Factory\\:\\:getConfig\\(\\) should return int\\<0, 1023\\> but returns int\\.$#"
message: "#^Method JsonSchema\\\\Constraints\\\\Factory\\:\\:getConfig\\(\\) should return int\\<0, 2047\\> but returns int\\.$#"
count: 1
path: src/JsonSchema/Constraints/Factory.php

-
message: "#^Property JsonSchema\\\\Constraints\\\\Factory\\:\\:\\$checkMode \\(int\\<0, 1023\\>\\) does not accept int\\.$#"
message: "#^Property JsonSchema\\\\Constraints\\\\Factory\\:\\:\\$checkMode \\(int\\<0, 2047\\>\\) does not accept int\\.$#"
count: 2
path: src/JsonSchema/Constraints/Factory.php

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 @@ -23,6 +23,7 @@ abstract class Constraint extends BaseConstraint implements ConstraintInterface
public const CHECK_MODE_ONLY_REQUIRED_DEFAULTS = 0x00000080;
public const CHECK_MODE_VALIDATE_SCHEMA = 0x00000100;
public const CHECK_MODE_STRICT = 0x00000200;
public const CHECK_MODE_FAIL_FAST = 0x00000400;

/**
* Bubble down the path
Expand Down
24 changes: 24 additions & 0 deletions src/JsonSchema/Constraints/Drafts/Draft06/AnyOfConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,30 @@ public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = n
return;
}

// Only the validity of each branch is needed to know whether any of them matches. The errors of the
// branches are collected afterwards, by evaluating them once more, only if the anyOf fails.
$previousFailFast = $this->factory->startFailFast();
if ($previousFailFast !== null) {
try {
foreach ($schema->anyOf as $anyOfSchema) {
$schemaConstraint = $this->factory->createInstanceFor('schema');
$schemaConstraint->check($value, $anyOfSchema, $path, $i);
if ($schemaConstraint->isValid()) {
return;
}
}
} finally {
$this->factory->stopFailFast($previousFailFast);
}

if ($previousFailFast) {
// Only the validity of this anyOf is needed as well
$this->addError(ConstraintError::ANY_OF(), $path);

return;
}
}

foreach ($schema->anyOf as $anyOfSchema) {
$schemaConstraint = $this->factory->createInstanceFor('schema');

Expand Down
15 changes: 13 additions & 2 deletions src/JsonSchema/Constraints/Drafts/Draft06/Draft06Constraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,17 @@ class Draft06Constraint extends Constraint
{
public function __construct(?\JsonSchema\Constraints\Factory $factory = null)
{
parent::__construct(new Factory(
$draftFactory = new Factory(
$factory ? $factory->getSchemaStorage() : new SchemaStorage(),
$factory ? $factory->getUriRetriever() : new UriRetriever(),
$factory ? $factory->getConfig() : Constraint::CHECK_MODE_NORMAL
));
);
if ($factory) {
// Nested schemas take part in the evaluation of the schema they belong to
$draftFactory->setEvaluationState($factory->getEvaluationState());
}

parent::__construct($draftFactory);
}

public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = null): void
Expand Down Expand Up @@ -134,6 +140,11 @@ private function validateSchema($schema, ?JsonPointer $path): void
*/
protected function checkForKeyword(string $keyword, $value, $schema = null, ?JsonPointer $path = null, $i = null): void
{
// The schema is invalid already, and only its validity is needed
if ($this->numErrors() > 0 && $this->factory->isFailFast()) {
return;
}

$validator = $this->factory->createInstanceFor($keyword);
$validator->check($value, $schema, $path, $i);

Expand Down
3 changes: 3 additions & 0 deletions src/JsonSchema/Constraints/Drafts/Draft06/ItemsConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = n
}

$this->addErrors($schemaConstraint->getErrors());
if ($this->factory->isFailFast()) {
return;
}
}
}
}
12 changes: 10 additions & 2 deletions src/JsonSchema/Constraints/Drafts/Draft06/NotConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,16 @@ public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = n
return;
}

$schemaConstraint = $this->factory->createInstanceFor('schema');
$schemaConstraint->check($value, $schema->not, $path, $i);
// Only the validity of the subschema is needed, its errors are never reported
$previousFailFast = $this->factory->startFailFast();
try {
$schemaConstraint = $this->factory->createInstanceFor('schema');
$schemaConstraint->check($value, $schema->not, $path, $i);
} finally {
if ($previousFailFast !== null) {
$this->factory->stopFailFast($previousFailFast);
}
}

if (!$schemaConstraint->isValid()) {
return;
Expand Down
29 changes: 29 additions & 0 deletions src/JsonSchema/Constraints/Drafts/Draft06/OneOfConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,35 @@ public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = n
return;
}

// Only the validity of each branch is needed to know whether exactly one of them matches. The errors of
// the branches are collected afterwards, by evaluating them once more, only if the oneOf fails.
$previousFailFast = $this->factory->startFailFast();
if ($previousFailFast !== null) {
$matchedSchema = 0;
try {
foreach ($schema->oneOf as $oneOfSchema) {
$schemaConstraint = $this->factory->createInstanceFor('schema');
$schemaConstraint->check($value, $oneOfSchema, $path, $i);
if ($schemaConstraint->isValid()) {
$matchedSchema++;
}
}
} finally {
$this->factory->stopFailFast($previousFailFast);
}

if ($matchedSchema === 1) {
return;
}

if ($previousFailFast) {
// Only the validity of this oneOf is needed as well
$this->addError(ConstraintError::ONE_OF(), $path);

return;
}
}

$matchedSchema = 0;
foreach ($schema->oneOf as $oneOfSchema) {
$schemaConstraint = $this->factory->createInstanceFor('schema');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = n
}

$this->addErrors($schemaConstraint->getErrors());
if ($this->factory->isFailFast()) {
return;
}
}
}
}
24 changes: 24 additions & 0 deletions src/JsonSchema/Constraints/Drafts/Draft07/AnyOfConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,30 @@ public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = n
return;
}

// Only the validity of each branch is needed to know whether any of them matches. The errors of the
// branches are collected afterwards, by evaluating them once more, only if the anyOf fails.
$previousFailFast = $this->factory->startFailFast();
if ($previousFailFast !== null) {
try {
foreach ($schema->anyOf as $anyOfSchema) {
$schemaConstraint = $this->factory->createInstanceFor('schema');
$schemaConstraint->check($value, $anyOfSchema, $path, $i);
if ($schemaConstraint->isValid()) {
return;
}
}
} finally {
$this->factory->stopFailFast($previousFailFast);
}

if ($previousFailFast) {
// Only the validity of this anyOf is needed as well
$this->addError(ConstraintError::ANY_OF(), $path);

return;
}
}

foreach ($schema->anyOf as $anyOfSchema) {
$schemaConstraint = $this->factory->createInstanceFor('schema');

Expand Down
15 changes: 13 additions & 2 deletions src/JsonSchema/Constraints/Drafts/Draft07/Draft07Constraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,17 @@ class Draft07Constraint extends Constraint
{
public function __construct(?\JsonSchema\Constraints\Factory $factory = null)
{
parent::__construct(new Factory(
$draftFactory = new Factory(
$factory ? $factory->getSchemaStorage() : new SchemaStorage(),
$factory ? $factory->getUriRetriever() : new UriRetriever(),
$factory ? $factory->getConfig() : Constraint::CHECK_MODE_NORMAL
));
);
if ($factory) {
// Nested schemas take part in the evaluation of the schema they belong to
$draftFactory->setEvaluationState($factory->getEvaluationState());
}

parent::__construct($draftFactory);
}

public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = null): void
Expand Down Expand Up @@ -136,6 +142,11 @@ private function validateSchema($schema, ?JsonPointer $path): void
*/
protected function checkForKeyword(string $keyword, $value, $schema = null, ?JsonPointer $path = null, $i = null): void
{
// The schema is invalid already, and only its validity is needed
if ($this->numErrors() > 0 && $this->factory->isFailFast()) {
return;
}

$validator = $this->factory->createInstanceFor($keyword);
$validator->check($value, $schema, $path, $i);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,15 @@ public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = n
$ifSchema = $schema->if;

if (!is_bool($ifSchema)) {
$schemaConstraint->check($value, $ifSchema, $path, $i);
// Only the validity of the if subschema is needed, its errors are never reported
$previousFailFast = $this->factory->startFailFast();
try {
$schemaConstraint->check($value, $ifSchema, $path, $i);
} finally {
if ($previousFailFast !== null) {
$this->factory->stopFailFast($previousFailFast);
}
}
$meetsIfConditions = $schemaConstraint->isValid();
$schemaConstraint->reset();
} else {
Expand Down
3 changes: 3 additions & 0 deletions src/JsonSchema/Constraints/Drafts/Draft07/ItemsConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = n
}

$this->addErrors($schemaConstraint->getErrors());
if ($this->factory->isFailFast()) {
return;
}
}
}
}
12 changes: 10 additions & 2 deletions src/JsonSchema/Constraints/Drafts/Draft07/NotConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,16 @@ public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = n
return;
}

$schemaConstraint = $this->factory->createInstanceFor('schema');
$schemaConstraint->check($value, $schema->not, $path, $i);
// Only the validity of the subschema is needed, its errors are never reported
$previousFailFast = $this->factory->startFailFast();
try {
$schemaConstraint = $this->factory->createInstanceFor('schema');
$schemaConstraint->check($value, $schema->not, $path, $i);
} finally {
if ($previousFailFast !== null) {
$this->factory->stopFailFast($previousFailFast);
}
}

if (!$schemaConstraint->isValid()) {
return;
Expand Down
29 changes: 29 additions & 0 deletions src/JsonSchema/Constraints/Drafts/Draft07/OneOfConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,35 @@ public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = n
return;
}

// Only the validity of each branch is needed to know whether exactly one of them matches. The errors of
// the branches are collected afterwards, by evaluating them once more, only if the oneOf fails.
$previousFailFast = $this->factory->startFailFast();
if ($previousFailFast !== null) {
$matchedSchema = 0;
try {
foreach ($schema->oneOf as $oneOfSchema) {
$schemaConstraint = $this->factory->createInstanceFor('schema');
$schemaConstraint->check($value, $oneOfSchema, $path, $i);
if ($schemaConstraint->isValid()) {
$matchedSchema++;
}
}
} finally {
$this->factory->stopFailFast($previousFailFast);
}

if ($matchedSchema === 1) {
return;
}

if ($previousFailFast) {
// Only the validity of this oneOf is needed as well
$this->addError(ConstraintError::ONE_OF(), $path);

return;
}
}

$matchedSchema = 0;
foreach ($schema->oneOf as $oneOfSchema) {
$schemaConstraint = $this->factory->createInstanceFor('schema');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = n
}

$this->addErrors($schemaConstraint->getErrors());
if ($this->factory->isFailFast()) {
return;
}
}
}
}
24 changes: 24 additions & 0 deletions src/JsonSchema/Constraints/Drafts/Draft2019/AnyOfConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,30 @@ public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = n
return;
}

// Only the validity of each branch is needed to know whether any of them matches. The errors of the
// branches are collected afterwards, by evaluating them once more, only if the anyOf fails.
$previousFailFast = $this->factory->startFailFast();
if ($previousFailFast !== null) {
try {
foreach ($schema->anyOf as $anyOfSchema) {
$schemaConstraint = $this->factory->createInstanceFor('schema');
$schemaConstraint->check($value, $anyOfSchema, $path, $i);
if ($schemaConstraint->isValid()) {
return;
}
}
} finally {
$this->factory->stopFailFast($previousFailFast);
}

if ($previousFailFast) {
// Only the validity of this anyOf is needed as well
$this->addError(ConstraintError::ANY_OF(), $path);

return;
}
}

foreach ($schema->anyOf as $anyOfSchema) {
$schemaConstraint = $this->factory->createInstanceFor('schema');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,17 @@ class Draft2019Constraint extends Constraint
{
public function __construct(?\JsonSchema\Constraints\Factory $factory = null)
{
parent::__construct(new Factory(
$draftFactory = new Factory(
$factory ? $factory->getSchemaStorage() : new SchemaStorage(),
$factory ? $factory->getUriRetriever() : new UriRetriever(),
$factory ? $factory->getConfig() : Constraint::CHECK_MODE_NORMAL
));
);
if ($factory) {
// Nested schemas take part in the evaluation of the schema they belong to
$draftFactory->setEvaluationState($factory->getEvaluationState());
}

parent::__construct($draftFactory);
}

public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = null): void
Expand Down Expand Up @@ -136,6 +142,11 @@ private function validateSchema($schema, ?JsonPointer $path): void
*/
protected function checkForKeyword(string $keyword, $value, $schema = null, ?JsonPointer $path = null, $i = null): void
{
// The schema is invalid already, and only its validity is needed
if ($this->numErrors() > 0 && $this->factory->isFailFast()) {
return;
}

$validator = $this->factory->createInstanceFor($keyword);
$validator->check($value, $schema, $path, $i);

Expand Down
Loading