Something went wrong. Try again.
Parse and validate AT Protocol Lexicons with DTO generation for Laravel
Something went wrong. Try again.
2.8 kB · 97 lines
PHP
at main
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798<?php
namespace SocialDept\AtpSchema\Console;
use Illuminate\Console\Command;use SocialDept\AtpSchema\Parser\SchemaLoader;use SocialDept\AtpSchema\Validation\LexiconValidator;
class ValidateCommand extends Command{ /** * The name and signature of the console command. */ protected $signature = 'schema:validate {nsid : The NSID of the schema to validate} {--data= : JSON data to validate against the schema} {--file= : Path to file containing JSON data to validate}';
/** * The console command description. */ protected $description = 'Validate data against ATProto Lexicon schemas';
/** * Execute the console command. */ public function handle(): int { $nsid = $this->argument('nsid'); $dataJson = $this->option('data'); $dataFile = $this->option('file');
if (! $dataJson && ! $dataFile) { $this->error('Either --data or --file option must be provided');
return self::FAILURE; }
try { // Load data if ($dataFile) { if (! file_exists($dataFile)) { $this->error("File not found: {$dataFile}");
return self::FAILURE; }
$dataJson = file_get_contents($dataFile); }
$data = json_decode($dataJson, true);
if (json_last_error() !== JSON_ERROR_NONE) { $this->error('Invalid JSON: '.json_last_error_msg());
return self::FAILURE; }
// Load schema and validate $sources = config('schema.sources', []); $loader = new SchemaLoader($sources); $validator = new LexiconValidator($loader);
$this->info("Validating data against schema: {$nsid}");
$document = $loader->load($nsid);
$errors = $validator->validateWithErrors($data, $document);
if (empty($errors)) { $this->info('✓ Validation passed');
return self::SUCCESS; }
$this->error('✗ Validation failed:'); $this->newLine();
foreach ($errors as $field => $fieldErrors) { $this->line(" {$field}:"); foreach ($fieldErrors as $error) { $this->line(" - {$error}"); } }
return self::FAILURE; } catch (\Exception $e) { $this->error('Validation error: '.$e->getMessage());
if ($this->output->isVerbose()) { $this->error($e->getTraceAsString()); }
return self::FAILURE; } }}