validateRecord( 'com.example.post', [ '$type' => 'com.example.post', 'text' => 'Hello world', 'meta' => ['label' => 'example'], ], self::schemaWithRefs(), ); static::assertTrue($result->isValid()); } public function testValidatesLocalRefs(): void { $result = new RecordValidator()->validateRecord( 'com.example.post', [ '$type' => 'com.example.post', 'text' => 'Hello', 'meta' => ['label' => 123], ], self::schemaWithRefs(), ); static::assertFalse($result->isValid()); static::assertSame(['$.meta.label'], \array_column($result->violations, 'path')); } public function testValidatesExternalRefs(): void { $validator = new RecordValidator(static fn (string $nsid): ?array => ( $nsid === 'com.example.profile' ? [ 'lexicon' => 1, 'id' => 'com.example.profile', 'defs' => [ 'main' => [ 'type' => 'object', 'required' => ['handle'], 'properties' => [ 'handle' => ['type' => 'string'], ], ], ], ] : NULL )); $result = $validator->validateRecord( 'com.example.post', [ '$type' => 'com.example.post', 'text' => 'Hello', 'author' => [], ], self::schemaWithRefs(), ); static::assertFalse($result->isValid()); static::assertSame(['$.author.handle'], \array_column($result->violations, 'path')); } public function testAcceptsValuesThroughExternalRefs(): void { $validator = new RecordValidator(static fn (string $nsid): ?array => $nsid === 'com.example.profile' ? self::profileLexicon() : NULL); $result = $validator->validateRecord( 'com.example.post', [ '$type' => 'com.example.post', 'text' => 'Hello world', 'author' => ['handle' => 'alice.test.example.com'], ], self::schemaWithRefs(), ); static::assertTrue($result->isValid()); } public function testReportsUnresolvedExternalRefs(): void { $result = new RecordValidator()->validateRecord( 'com.example.post', [ '$type' => 'com.example.post', 'text' => 'Hello world', 'author' => ['handle' => 'alice.test.example.com'], ], self::schemaWithRefs(), ); static::assertFalse($result->isValid()); static::assertSame(['$.author'], \array_column($result->violations, 'path')); static::assertSame(['unresolved_ref'], \array_column($result->violations, 'code')); } /** * @return array */ private static function schemaWithRefs(): array { return [ 'lexicon' => 1, 'id' => 'com.example.post', 'defs' => [ 'main' => [ 'type' => 'record', 'key' => 'tid', 'record' => [ 'type' => 'object', 'required' => ['text'], 'properties' => [ 'text' => ['type' => 'string'], 'meta' => ['ref' => '#metadata'], 'author' => ['ref' => 'com.example.profile'], ], ], ], 'metadata' => [ 'type' => 'object', 'required' => ['label'], 'properties' => [ 'label' => ['type' => 'string'], ], ], ], ]; } /** @return array */ private static function profileLexicon(): array { return [ 'lexicon' => 1, 'id' => 'com.example.profile', 'defs' => [ 'main' => [ 'type' => 'object', 'required' => ['handle'], 'properties' => [ 'handle' => ['type' => 'string', 'format' => 'handle'], ], ], ], ]; } }