|null $repository */ public function __construct( LexiconRepositoryInterface|callable|null $repository = NULL, private readonly LexiconParser $parser = new LexiconParser(), private readonly NsidParser $nsidParser = new NsidParser(), ) { $this->repository = match (TRUE) { $repository instanceof LexiconRepositoryInterface => $repository, $repository === NULL => new InMemoryLexiconRepository(), default => new CallbackLexiconRepository($repository, $this->parser), }; } /** * Validates the record. * * @param array $record * @param array $schema */ public function validateRecord( string $collection, array $record, array $schema, ?ValidationOptions $options = NULL, ?string $recordKey = NULL, ): ValidationResult { $documentResult = $this->parser->parse($this->normalizeDocumentSchema($collection, $schema)); if (!$documentResult->isValid() || !$documentResult->value instanceof LexiconDocument) { return new ValidationResult($documentResult->violations); } if ( !$this->nsidParser->isValid($collection) || $documentResult->value->id !== $this->nsidParser->normalize($collection) ) { return new ValidationResult([ new ValidationViolation( '$.id', \sprintf('Lexicon id "%s" does not match collection "%s".', $documentResult->value->id, $collection), 'lexicon_id_mismatch', ), ]); } $repository = new InMemoryLexiconRepository(); $repository->add($documentResult->value); $compiler = new SchemaCompiler( new OverlayLexiconRepository($repository, $this->repository), validateUnknownDataModel: TRUE, ); $recordSchema = $compiler->compileRecord($collection); if ($recordSchema === NULL) { return new ValidationResult([ new ValidationViolation( '$', \sprintf('No record schema found for collection "%s".', $collection), 'unknown_lexicon', ), ]); } return self::validateCompiledRecord($recordSchema, $record, $options, $recordKey); } /** * Validates a record by resolving its collection from this validator's repository. * * @param array $record */ public function validateRepositoryRecord( string $collection, array $record, ?ValidationOptions $options = NULL, ?string $recordKey = NULL, ): ValidationResult { $compiler = new SchemaCompiler($this->repository, validateUnknownDataModel: TRUE); $recordSchema = $compiler->compileRecord($collection); if ($recordSchema === NULL) { return new ValidationResult([ new ValidationViolation( '$', \sprintf('No configured lexicon found for collection "%s".', $collection), 'unknown_lexicon', ), ]); } return self::validateCompiledRecord($recordSchema, $record, $options, $recordKey); } /** * @param array $record */ private static function validateCompiledRecord( Schema\RecordSchema $schema, array $record, ?ValidationOptions $options, ?string $recordKey, ): ValidationResult { $options ??= ValidationOptions::validate(); $recordResult = $schema->safeValidate($record, $options); if ($recordKey === NULL) { return $recordResult; } $keyResult = $schema->validateKey($recordKey, $options); $keyViolations = \array_map( static fn (ValidationViolation $violation): ValidationViolation => new ValidationViolation( '$rkey', $violation->message, $violation->code, ), $keyResult->violations, ); return new ValidationResult( [...$keyViolations, ...$recordResult->violations], $recordResult->value, ); } /** * @param array $schema * * @return array */ private static function normalizeDocumentSchema(string $collection, array $schema): array { if (isset($schema['id'], $schema['defs']) && \is_string($schema['id']) && \is_array($schema['defs'])) { return $schema + ['lexicon' => 1]; } if (($schema['type'] ?? NULL) === 'record') { return [ 'lexicon' => 1, 'id' => $collection, 'defs' => ['main' => $schema], ]; } return [ 'lexicon' => 1, 'id' => $collection, 'defs' => [ 'main' => [ 'type' => 'record', 'key' => 'any', 'record' => $schema, ], ], ]; } }