Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168<?php
declare(strict_types=1);
namespace Wotnak\Atproto\Lexicon;
use Wotnak\Atproto\Identifier\NsidParser;
/** * Validates ATProto record payloads against Lexicon schemas. * * @api */final class RecordValidator {
private readonly LexiconRepositoryInterface $repository;
/** * @param \Wotnak\Atproto\Lexicon\LexiconRepositoryInterface|callable(string): ?array<string, mixed>|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<string, mixed> $record * @param array<string, mixed> $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<string, mixed> $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<string, mixed> $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<string, mixed> $schema * * @return array<string, mixed> */ 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, ], ], ]; }
}