validateRecord( 'app.bsky.feed.post', [ '$type' => 'app.bsky.feed.post', 'text' => 'Hello world', 'count' => 5, 'tags' => [], ], self::schema(), ); static::assertTrue($result->isValid()); } public function testReportsMissingRequiredAndInvalidTypes(): void { $result = new RecordValidator()->validateRecord( 'app.bsky.feed.post', [ '$type' => 'app.bsky.feed.like', 'text' => 123, 'count' => 20, 'tags' => ['ok', 99], ], self::schema(), ); static::assertFalse($result->isValid()); static::assertSame( [ '$type', '$.text', '$.count', '$.tags[1]', ], \array_map(static fn ($violation): string => $violation->path, $result->violations), ); } public function testRequiresRecordType(): void { $result = new RecordValidator()->validateRecord( 'app.bsky.feed.post', ['text' => 'Hello world'], self::schema(), ); static::assertFalse($result->isValid()); static::assertSame(['$type'], \array_column($result->violations, 'path')); } public function testUnknownFieldsMustStillFollowTheDataModel(): void { $result = new RecordValidator()->validateRecord( 'app.bsky.feed.post', [ '$type' => 'app.bsky.feed.post', 'text' => 'Hello world', 'futureField' => 1.5, ], self::schema(), ); static::assertFalse($result->isValid()); static::assertSame(['$.futureField'], \array_column($result->violations, 'path')); static::assertSame(['invalid_lex_value'], \array_column($result->violations, 'code')); } public function testBuildAddsAndOverwritesTypeWithoutMutatingInput(): void { $schema = self::compiledRecordSchema(); $input = ['$type' => 'app.bsky.feed.like', 'text' => 'Hello world']; $built = $schema->build($input); static::assertSame( [ '$type' => 'app.bsky.feed.post', 'text' => 'Hello world', ], $built, ); static::assertSame('app.bsky.feed.like', $input['$type']); static::assertTrue($schema->safeValidate($built)->isValid()); } public function testExposesConfiguredRecordKeyType(): void { static::assertSame('tid', self::compiledRecordSchema()->key()); static::assertSame( 'any', new RecordSchema('app.bsky.feed.post', 'any', new ObjectSchema([]))->key(), ); static::assertSame( 'literal:self', new RecordSchema('app.bsky.feed.post', 'literal:self', new ObjectSchema([]))->key(), ); } public function testValidatesAnyRecordKeys(): void { $schema = new RecordSchema('app.bsky.feed.post', 'any', new ObjectSchema([])); static::assertTrue($schema->validateKey('anyStringKey')->isValid()); static::assertTrue($schema->validateKey('key123')->isValid()); static::assertTrue($schema->validateKey('key-with-dashes')->isValid()); static::assertFalse($schema->validateKey('')->isValid()); static::assertFalse($schema->validateKey('invalid/key')->isValid()); static::assertFalse($schema->validateKey(123)->isValid()); } public function testValidatesTidRecordKeys(): void { $schema = self::compiledRecordSchema(); static::assertTrue($schema->keySchema()->safeValidate('3jzfcijpj2z2a')->isValid()); static::assertFalse($schema->validateKey('not-a-tid')->isValid()); static::assertFalse($schema->validateKey('3jzfcijpj2z2!')->isValid()); static::assertFalse($schema->validateKey('')->isValid()); static::assertFalse($schema->validateKey('regularString')->isValid()); } public function testValidatesNsidRecordKeys(): void { $schema = new RecordSchema('app.bsky.feed.post', 'nsid', new ObjectSchema([])); static::assertTrue($schema->validateKey('app.bsky.feed.post')->isValid()); static::assertTrue($schema->validateKey('com.example.app.feature.action')->isValid()); static::assertFalse($schema->validateKey('not-an-nsid')->isValid()); static::assertFalse($schema->validateKey('app.bsky.feed!')->isValid()); static::assertFalse($schema->validateKey('')->isValid()); } public function testValidatesSelfLiteralRecordKeys(): void { $schema = new RecordSchema('app.bsky.feed.post', 'literal:self', new ObjectSchema([])); static::assertTrue($schema->keySchema()->safeParse('self')->isValid()); static::assertFalse($schema->keySchema()->safeParse('other')->isValid()); static::assertFalse($schema->keySchema()->safeParse('Self')->isValid()); static::assertFalse($schema->keySchema()->safeParse('')->isValid()); } public function testValidatesLiteralRecordKeys(): void { $schema = new RecordSchema('app.bsky.feed.post', 'literal:customKey', new ObjectSchema([])); static::assertTrue($schema->validateKey('customKey')->isValid()); static::assertFalse($schema->validateKey('otherKey')->isValid()); static::assertFalse($schema->validateKey('custom')->isValid()); static::assertSame('customKey', $schema->keySchema()->safeParse(NULL)->value); } public function testValidatesRecordTypesWithHashFragments(): void { $schema = new RecordSchema( 'app.bsky.feed.post#main', 'any', new ObjectSchema(['text' => new StringSchema()], required: ['text']), ); $valid = $schema->safeValidate([ '$type' => 'app.bsky.feed.post#main', 'text' => 'Hello world', ]); $withoutHash = $schema->safeValidate([ '$type' => 'app.bsky.feed.post', 'text' => 'Hello world', ]); $differentHash = $schema->safeValidate([ '$type' => 'app.bsky.feed.post#other', 'text' => 'Hello world', ]); static::assertTrue($valid->isValid()); static::assertFalse($withoutHash->isValid()); static::assertFalse($differentHash->isValid()); } public function testRejectsUnsupportedRecordKeyTypes(): void { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('Unsupported record key type'); new RecordSchema('app.bsky.feed.post', 'literal:invalid/key', new ObjectSchema([])); } public function testRecordValidatorValidatesOptionalRecordKey(): void { $repository = new InMemoryLexiconRepository(); $repository->add(new LexiconDocument( 'app.bsky.feed.post', [ 'main' => [ 'type' => 'record', 'key' => 'tid', 'record' => [ 'type' => 'object', 'required' => ['text'], 'properties' => ['text' => ['type' => 'string']], ], ], ], [], )); $validator = new RecordValidator($repository); $record = ['$type' => 'app.bsky.feed.post', 'text' => 'Hello world']; $valid = $validator->validateRepositoryRecord( 'app.bsky.feed.post', $record, recordKey: '3jzfcijpj2z2a', ); $invalid = $validator->validateRepositoryRecord( 'app.bsky.feed.post', $record, recordKey: 'not-a-tid', ); static::assertTrue($valid->isValid()); static::assertFalse($invalid->isValid()); static::assertSame(['$rkey'], \array_column($invalid->violations, 'path')); static::assertSame(['invalid_format'], \array_column($invalid->violations, 'code')); } public function testRejectsNonStringNullAndNonNormalizedTypes(): void { $schema = self::compiledRecordSchema(); foreach ([NULL, 42, TRUE, [], 'APP.BSKY.FEED.POST', 'app.bsky.feed.post#main'] as $type) { $result = $schema->safeValidate(['$type' => $type, 'text' => 'Hello world']); static::assertFalse($result->isValid()); static::assertContains('$type', \array_column($result->violations, 'path')); } } private static function compiledRecordSchema(): RecordSchema { return new RecordSchema( 'app.bsky.feed.post', 'tid', new ObjectSchema(['text' => new StringSchema()], required: ['text']), ); } /** * @return array */ private static function schema(): array { return [ 'type' => 'object', 'required' => ['text'], 'properties' => [ 'text' => [ 'type' => 'string', 'maxLength' => 100, ], 'count' => [ 'type' => 'integer', 'minimum' => 1, 'maximum' => 10, ], 'tags' => [ 'type' => 'array', 'items' => ['type' => 'string'], ], ], ]; } }