diff --git a/src/Schemas/ObjectSchema.php b/src/Schemas/ObjectSchema.php index 1cc11be..be73c06 100644 --- a/src/Schemas/ObjectSchema.php +++ b/src/Schemas/ObjectSchema.php @@ -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; @@ -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 + ); } -} \ No newline at end of file +}