Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUGFIX] Don't throw exceptions until after checking anyOf / oneOf #394

Merged
merged 1 commit into from
Mar 21, 2017
Merged
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
27 changes: 19 additions & 8 deletions src/JsonSchema/Constraints/UndefinedConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use JsonSchema\ConstraintError;
use JsonSchema\Constraints\TypeCheck\LooseTypeCheck;
use JsonSchema\Entity\JsonPointer;
use JsonSchema\Exception\ValidationException;
use JsonSchema\Uri\UriResolver;

/**
Expand Down Expand Up @@ -245,11 +246,16 @@ protected function validateOfProperties(&$value, $schema, JsonPointer $path, $i
if (isset($schema->anyOf)) {
$isValid = false;
$startErrors = $this->getErrors();
$caughtException = null;
foreach ($schema->anyOf as $anyOf) {
$initErrors = $this->getErrors();
$this->checkUndefined($value, $anyOf, $path, $i);
if ($isValid = (count($this->getErrors()) == count($initErrors))) {
break;
try {
$this->checkUndefined($value, $anyOf, $path, $i);
if ($isValid = (count($this->getErrors()) == count($initErrors))) {
break;
}
} catch (ValidationException $e) {
$isValid = false;
}
}
if (!$isValid) {
Expand All @@ -264,12 +270,17 @@ protected function validateOfProperties(&$value, $schema, JsonPointer $path, $i
$matchedSchemas = 0;
$startErrors = $this->getErrors();
foreach ($schema->oneOf as $oneOf) {
$this->errors = array();
$this->checkUndefined($value, $oneOf, $path, $i);
if (count($this->getErrors()) == 0) {
$matchedSchemas++;
try {
$this->errors = array();
$this->checkUndefined($value, $oneOf, $path, $i);
if (count($this->getErrors()) == 0) {
$matchedSchemas++;
}
$allErrors = array_merge($allErrors, array_values($this->getErrors()));
} catch (ValidationException $e) {
// deliberately do nothing here - validation failed, but we want to check
// other schema options in the OneOf field.
}
$allErrors = array_merge($allErrors, array_values($this->getErrors()));
}
if ($matchedSchemas !== 1) {
$this->addErrors(array_merge($allErrors, $startErrors));
Expand Down
43 changes: 43 additions & 0 deletions tests/Constraints/OfPropertiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

namespace JsonSchema\Tests\Constraints;

use JsonSchema\Constraints\Constraint;
use JsonSchema\Validator;

/**
* Class OfPropertiesTest
*/
Expand Down Expand Up @@ -223,4 +226,44 @@ public function getInvalidTests()
)
);
}

public function testNoPrematureAnyOfException()
{
$schema = json_decode('{
"type": "object",
"properties": {
"propertyOne": {
"anyOf": [
{"type": "number"},
{"type": "string"}
]
}
}
}');
$data = json_decode('{"propertyOne":"ABC"}');

$v = new Validator();
$v->validate($data, $schema, Constraint::CHECK_MODE_EXCEPTIONS);
$this->assertTrue($v->isValid());
}

public function testNoPrematureOneOfException()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Am I seeing things or is this method identical to testNoPrematureAnyOfException?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're not seeing things - thanks for spotting that one! Will sort once I'm back at my desk later today.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bighappyface Fixed.

{
$schema = json_decode('{
"type": "object",
"properties": {
"propertyOne": {
"oneOf": [
{"type": "number"},
{"type": "string"}
]
}
}
}');
$data = json_decode('{"propertyOne":"ABC"}');

$v = new Validator();
$v->validate($data, $schema, Constraint::CHECK_MODE_EXCEPTIONS);
$this->assertTrue($v->isValid());
}
}