safeValidate([ '$type' => 'app.bsky.actor.person', 'name' => 'Alice', 'age' => 30, ])->isValid(), ); static::assertTrue( $schema->safeValidate([ '$type' => 'app.bsky.feed.post', 'text' => 'Hello world', 'createdAt' => '2023-01-01T00:00:00Z', ])->isValid(), ); } public function testKnownVariantMustSatisfyItsReferencedSchema(): void { $schema = self::typedUnion(TRUE); $wrongType = $schema->safeValidate([ '$type' => 'app.bsky.actor.person', 'name' => 'Alice', 'age' => 'thirty', ]); static::assertFalse($wrongType->isValid()); static::assertSame(['$.age'], \array_column($wrongType->violations, 'path')); $missing = $schema->safeValidate([ '$type' => 'app.bsky.actor.person', 'name' => 'Alice', ]); static::assertFalse($missing->isValid()); static::assertSame(['$.age'], \array_column($missing->violations, 'path')); } public function testOpenAndClosedUnionsHandleUnknownVariantsDifferently(): void { $value = [ '$type' => 'app.bsky.feed.like', 'subject' => 'some-uri', 'createdAt' => '2023-01-01T00:00:00Z', ]; static::assertFalse(self::typedUnion(TRUE)->safeValidate($value)->isValid()); static::assertTrue(self::typedUnion(FALSE)->safeValidate($value)->isValid()); } #[\PHPUnit\Framework\Attributes\DataProvider('missingTypeProvider')] public function testRequiresStringTypeDiscriminator(mixed $value): void { $result = self::typedUnion(FALSE)->safeValidate($value); static::assertFalse($result->isValid()); static::assertSame(['missing_type'], \array_column($result->violations, 'code')); } /** @return array */ public static function missingTypeProvider(): array { return [ 'missing type' => [['name' => 'Alice', 'age' => 30]], 'null type' => [['$type' => NULL]], 'numeric type' => [['$type' => 123]], 'non-object' => ['not an object'], 'null' => [NULL], 'list' => [[]], 'number' => [42], 'boolean' => [TRUE], ]; } public function testValidatesClosedUnionRefs(): void { $result = new RecordValidator()->validateRecord( 'com.example.post', [ '$type' => 'com.example.post', 'text' => 'Hello', 'embed' => ['$type' => 'com.example.unknown'], ], self::schemaWithUnion(), ); static::assertFalse($result->isValid()); static::assertSame(['$.embed'], \array_column($result->violations, 'path')); } public function testOpenUnionUnknownVariantStillFollowsTheDataModel(): void { $result = new RecordValidator()->validateRecord( 'com.example.post', [ '$type' => 'com.example.post', 'embed' => [ '$type' => 'com.example.futureEmbed', 'score' => 1.5, ], ], self::schemaWithProperty('embed', [ 'type' => 'union', 'refs' => [], ]), ); static::assertFalse($result->isValid()); static::assertSame(['$.embed.score'], \array_column($result->violations, 'path')); static::assertSame(['invalid_lex_value'], \array_column($result->violations, 'code')); } public function testMainDefinitionRequiresBareNsidDiscriminator(): void { $repository = new InMemoryLexiconRepository(); $repository->add(new LexiconDocument( 'com.example.variant', [ 'main' => [ 'type' => 'object', 'required' => ['value'], 'properties' => [ 'value' => ['type' => 'string'], ], ], ], [], )); $schema = new UnionSchema( new SchemaCompiler($repository), ['com.example.variant'], 'com.example.container', TRUE, ); $valid = $schema->safeValidate([ '$type' => 'com.example.variant', 'value' => 'valid', ]); $invalid = $schema->safeValidate([ '$type' => 'com.example.variant#main', 'value' => 'invalid', ]); static::assertTrue($valid->isValid()); static::assertFalse($invalid->isValid()); static::assertSame(['invalid_union_type'], \array_column($invalid->violations, 'code')); } /** * @param array $property * @return array */ private static function schemaWithProperty(string $name, array $property): array { return [ 'type' => 'object', 'required' => [$name], 'properties' => [$name => $property], ]; } /** * @return array */ private static function schemaWithUnion(): array { return [ 'lexicon' => 1, 'id' => 'com.example.post', 'defs' => [ 'main' => [ 'type' => 'record', 'key' => 'tid', 'record' => [ 'type' => 'object', 'required' => ['text'], 'properties' => [ 'text' => ['type' => 'string'], 'embed' => [ 'type' => 'union', 'closed' => TRUE, 'refs' => ['#knownEmbed'], ], ], ], ], 'knownEmbed' => [ 'type' => 'object', 'required' => ['name'], 'properties' => [ 'name' => ['type' => 'string'], ], ], ], ]; } private static function typedUnion(bool $closed): UnionSchema { $repository = new InMemoryLexiconRepository(); $repository->add( new LexiconDocument( 'app.bsky.actor.person', [ 'main' => [ 'type' => 'object', 'required' => ['name', 'age'], 'properties' => [ 'name' => ['type' => 'string'], 'age' => ['type' => 'integer'], ], ], ], [], ), ); $repository->add( new LexiconDocument( 'app.bsky.feed.post', [ 'main' => [ 'type' => 'object', 'required' => ['text', 'createdAt'], 'properties' => [ 'text' => ['type' => 'string'], 'createdAt' => ['type' => 'string'], ], ], ], [], ), ); return new UnionSchema( new SchemaCompiler($repository), ['app.bsky.actor.person', 'app.bsky.feed.post'], 'app.bsky.feed.container', $closed, ); } }