From 78385ab38fa558e5826a362980607e18061d9057 Mon Sep 17 00:00:00 2001 From: Andreas Wolf Date: Thu, 7 Aug 2025 16:00:17 +0200 Subject: [PATCH 1/2] Validate allOf properties after other errors --- src/Schemas/ObjectSchema.php | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/src/Schemas/ObjectSchema.php b/src/Schemas/ObjectSchema.php index 1b5da2c..607b1df 100644 --- a/src/Schemas/ObjectSchema.php +++ b/src/Schemas/ObjectSchema.php @@ -77,18 +77,28 @@ public function validate(ValidationContext $context): ?ValidationError */ public function doValidate(ValidationContext $context): ?ValidationError { + $errors = []; if ($this->before && ($error = $this->applyKeywords($this->before, $context))) { - return $error; + if ($context->stopAtFirstError()) { + return $error; + } + $errors[] = $error; } if ($this->types && ($type = $context->currentDataType())) { if (isset($this->types[$type]) && ($error = $this->applyKeywords($this->types[$type], $context))) { - return $error; + if ($context->stopAtFirstError()) { + return $error; + } + $errors[] = $error; } if (($type = Helper::getJsonSuperType($type)) && isset($this->types[$type])) { if ($error = $this->applyKeywords($this->types[$type], $context)) { - return $error; + if ($context->stopAtFirstError()) { + return $error; + } + $errors[] = $error; } } @@ -96,10 +106,21 @@ public function doValidate(ValidationContext $context): ?ValidationError } if ($this->after && ($error = $this->applyKeywords($this->after, $context))) { - return $error; + if ($context->stopAtFirstError()) { + return $error; + } + $errors[] = $error; + } + + if (count($errors) === 0) { + return null; + } + + if (count($errors) === 1) { + return $errors[0]; } - return null; + return new ValidationError('', $this, DataInfo::fromContext($context), 'Data must match schema', [], $errors); } /** @@ -145,4 +166,4 @@ protected function applyKeywords(array $keywords, ValidationContext $context): ? $error_list ); } -} \ No newline at end of file +} From aa413f447eab9d00ec1f77192367dd41ab644e68 Mon Sep 17 00:00:00 2001 From: Andreas Wolf Date: Thu, 7 Aug 2025 16:01:04 +0200 Subject: [PATCH 2/2] Collect errors for allOf keyword --- src/Keywords/AllOfKeyword.php | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/Keywords/AllOfKeyword.php b/src/Keywords/AllOfKeyword.php index cbc9c61..a339e4a 100644 --- a/src/Keywords/AllOfKeyword.php +++ b/src/Keywords/AllOfKeyword.php @@ -47,6 +47,8 @@ public function validate(ValidationContext $context, Schema $schema): ?Validatio { $object = $this->createArrayObject($context); + $errors = []; + foreach ($this->value as $index => $value) { if ($value === true) { continue; @@ -54,9 +56,13 @@ public function validate(ValidationContext $context, Schema $schema): ?Validatio if ($value === false) { $this->addEvaluatedFromArrayObject($object, $context); - return $this->error($schema, $context, 'allOf', 'The data should match all schemas', [ + $error = $this->error($schema, $context, 'allOf', 'The data should match all schemas', [ 'index' => $index, ]); + if ($context->stopAtFirstError()) { + return $error; + } + $errors[] = $error; } if (is_object($value) && !($value instanceof Schema)) { @@ -65,14 +71,24 @@ public function validate(ValidationContext $context, Schema $schema): ?Validatio if ($error = $context->validateSchemaWithoutEvaluated($value, null, false, $object)) { $this->addEvaluatedFromArrayObject($object, $context); - return $this->error($schema, $context, 'allOf', 'The data should match all schemas', [ + $error = $this->error($schema, $context, 'allOf', 'The data should match all schemas', [ 'index' => $index, ], $error); + if ($context->stopAtFirstError()) { + return $error; + } + $errors[] = $error; } } + if (count($errors) > 0 && !$context->stopAtFirstError()) { + return $this->error($schema, $context, 'allOf', 'The data should match all schemas', [ + 'index' => $index, + ], $errors); + } + $this->addEvaluatedFromArrayObject($object, $context); return null; } -} \ No newline at end of file +}