From fec2249c898e1518d82787452d2be7dc7223887d Mon Sep 17 00:00:00 2001 From: Guilliam Xavier Date: Fri, 16 Apr 2021 17:37:16 +0200 Subject: [PATCH 1/2] Keep errors order for oneOf like anyOf/allOf --- src/JsonSchema/Constraints/UndefinedConstraint.php | 3 ++- tests/Constraints/OfPropertiesTest.php | 8 ++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/JsonSchema/Constraints/UndefinedConstraint.php b/src/JsonSchema/Constraints/UndefinedConstraint.php index bdb95f4a..a29718ee 100644 --- a/src/JsonSchema/Constraints/UndefinedConstraint.php +++ b/src/JsonSchema/Constraints/UndefinedConstraint.php @@ -366,7 +366,8 @@ protected function validateOfProperties(&$value, $schema, JsonPointer $path, $i } } if ($matchedSchemas !== 1) { - $this->addErrors(array_merge($allErrors, $startErrors)); + $this->errors = array(); + $this->addErrors(array_merge($startErrors, $allErrors)); $this->addError(ConstraintError::ONE_OF(), $path); } else { $this->errors = $startErrors; diff --git a/tests/Constraints/OfPropertiesTest.php b/tests/Constraints/OfPropertiesTest.php index ff8bded3..8d78b8aa 100644 --- a/tests/Constraints/OfPropertiesTest.php +++ b/tests/Constraints/OfPropertiesTest.php @@ -79,11 +79,11 @@ public function getInvalidTests() array( 'property' => 'prop2', 'pointer' => '/prop2', - 'message' => 'Array value found, but a string is required', + 'message' => 'Array value found, but a number is required', 'constraint' => array( 'name' => 'type', 'params' => array( - 'expected' => 'a string', + 'expected' => 'a number', 'found' => 'array' ) ), @@ -92,11 +92,11 @@ public function getInvalidTests() array( 'property' => 'prop2', 'pointer' => '/prop2', - 'message' => 'Array value found, but a number is required', + 'message' => 'Array value found, but a string is required', 'constraint' => array( 'name' => 'type', 'params' => array( - 'expected' => 'a number', + 'expected' => 'a string', 'found' => 'array' ) ), From 095cbb06e1d4a44bd517f54f5b76e47a56b5ed9d Mon Sep 17 00:00:00 2001 From: Guilliam Xavier Date: Fri, 16 Apr 2021 17:38:24 +0200 Subject: [PATCH 2/2] Harmonize/simplify oneOf validation like anyOf --- src/JsonSchema/Constraints/UndefinedConstraint.php | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/JsonSchema/Constraints/UndefinedConstraint.php b/src/JsonSchema/Constraints/UndefinedConstraint.php index a29718ee..e2d64026 100644 --- a/src/JsonSchema/Constraints/UndefinedConstraint.php +++ b/src/JsonSchema/Constraints/UndefinedConstraint.php @@ -329,7 +329,6 @@ 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(); try { @@ -349,25 +348,21 @@ protected function validateOfProperties(&$value, $schema, JsonPointer $path, $i } if (isset($schema->oneOf)) { - $allErrors = array(); $matchedSchemas = 0; $startErrors = $this->getErrors(); foreach ($schema->oneOf as $oneOf) { + $initErrors = $this->getErrors(); try { - $this->errors = array(); $this->checkUndefined($value, $oneOf, $path, $i); - if (count($this->getErrors()) == 0) { + if (count($this->getErrors()) == count($initErrors)) { $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. } } if ($matchedSchemas !== 1) { - $this->errors = array(); - $this->addErrors(array_merge($startErrors, $allErrors)); $this->addError(ConstraintError::ONE_OF(), $path); } else { $this->errors = $startErrors;