$value */ #[DataProvider('validTypeProvider')] public function testAcceptsMissingOrMatchingType(array $value): void { static::assertTrue(self::schema()->safeValidate($value)->isValid()); } /** * @return array}> */ public static function validTypeProvider(): array { return [ 'missing type' => [['text' => 'Hello world']], 'matching type' => [[ '$type' => 'app.bsky.feed.post', 'text' => 'Hello world', ]], 'optional property' => [['text' => 'Hello world', 'likes' => 5]], ]; } public function testRejectsExplicitNullType(): void { static::assertFalse( self::schema() ->safeValidate([ '$type' => NULL, 'text' => 'Hello world', ]) ->isValid(), ); } public function testRejectsMismatchedAndNonNormalizedTypes(): void { foreach ([ 'app.bsky.feed.like', 'app.bsky.feed.post#main', 'app.bsky.feed.post#main-extra', 'APP.BSKY.FEED.POST', '', 123, ['value' => 'app.bsky.feed.post'], ] as $type) { $result = self::schema()->safeValidate(['$type' => $type, 'text' => 'Hello world']); static::assertFalse($result->isValid()); static::assertSame(['$.$type'], \array_column($result->violations, 'path')); static::assertSame(['type_mismatch'], \array_column($result->violations, 'code')); } } public function testDelegatesPropertyValidation(): void { $result = self::schema() ->safeValidate([ '$type' => 'app.bsky.feed.post', 'text' => 123, 'likes' => 'five', ]); static::assertFalse($result->isValid()); static::assertSame(['$.text', '$.likes'], \array_column($result->violations, 'path')); } #[DataProvider('nonObjectProvider')] public function testRejectsNonObjects(mixed $value): void { static::assertFalse(self::schema()->safeValidate($value)->isValid()); } /** * @return array */ public static function nonObjectProvider(): array { return [ 'null' => [NULL], 'string' => ['value'], 'integer' => [1], 'boolean' => [TRUE], 'list' => [['text', 5]], ]; } public function testSafeParseAppliesObjectDefaults(): void { $schema = new TypedObjectSchema( 'app.bsky.feed.post', new ObjectSchema(['text' => new StringSchema(default: 'Hello world')]), ); $result = $schema->safeParse(new \stdClass()); static::assertTrue($result->isValid()); static::assertEquals((object) ['text' => 'Hello world'], $result->value); } private static function schema(): TypedObjectSchema { return new TypedObjectSchema( 'app.bsky.feed.post', new ObjectSchema([ 'text' => new StringSchema(), 'likes' => new IntegerSchema(), ], required: ['text']), ); } }