Skip to content

Commit

Permalink
feat: opis#107 do not stop validation on first error
Browse files Browse the repository at this point in the history
  • Loading branch information
orakili committed Jul 4, 2024
1 parent c48df6d commit ee837f5
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions src/Schemas/ObjectSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
namespace Opis\JsonSchema\Schemas;

use Opis\JsonSchema\{Helper, Keyword, ValidationContext, KeywordValidator};
use Opis\JsonSchema\Info\DataInfo;
use Opis\JsonSchema\Info\SchemaInfo;
use Opis\JsonSchema\Errors\ValidationError;
use Opis\JsonSchema\KeywordValidators\CallbackKeywordValidator;
Expand Down Expand Up @@ -109,12 +110,31 @@ public function doValidate(ValidationContext $context): ?ValidationError
*/
protected function applyKeywords(array $keywords, ValidationContext $context): ?ValidationError
{
$errors = [];
foreach ($keywords as $keyword) {
if ($error = $keyword->validate($context, $this)) {
return $error;
$errors[] = $error;
}
}

return null;
if (empty($errors)) {
return null;
}

if (1 === count($errors)) {
return $errors[0];
}

/** @var \Opis\JsonSchema\Schema $schema */
$schema = $context->schema();

return new ValidationError(
'schema',
$schema,
DataInfo::fromContext($context),
'The data does not match the schema',
['data' => $context->currentData()],
$errors
);
}
}
}

0 comments on commit ee837f5

Please sign in to comment.