2, 'maxLength' => 4, ]) ->safeParse(self::value('com.example.stringLength', $value)); static::assertTrue($result->isValid(), \implode('; ', $result->messages())); } /** @return array */ public static function validByteLengthProvider(): array { return [ 'two ASCII bytes' => ['ab'], 'three ASCII bytes' => ['abc'], 'four ASCII bytes' => ['abcd'], 'combining acute accent' => ["\u{0301}"], 'ASCII plus combining accent' => ["a\u{0301}"], 'ASCII plus precomposed accent' => ['aé'], 'CJK character' => ['一'], 'two precomposed accents' => ['éé'], 'ASCII and precomposed accent' => ['aaé'], 'four-byte emoji' => ['👋'], ]; } #[DataProvider('invalidByteLengthProvider')] public function testRejectsStringsOutsideByteLengthConstraints(string $value, string $code): void { self::assertViolation( self::stringRecord('com.example.stringLength', [ 'minLength' => 2, 'maxLength' => 4, ]), self::value('com.example.stringLength', $value), $code, ); } /** @return array */ public static function invalidByteLengthProvider(): array { return [ 'empty string' => ['', 'min_length'], 'one ASCII byte' => ['a', 'min_length'], 'five ASCII bytes' => ['abcde', 'max_length'], 'ASCII plus two combining accents' => ["a\u{0301}\u{0301}", 'max_length'], 'three precomposed accents' => ['ééé', 'max_length'], 'emoji plus ASCII' => ['👋a', 'max_length'], 'two emoji' => ['👨👨', 'max_length'], 'family emoji' => ['👨‍👩‍👧‍👧', 'max_length'], ]; } #[DataProvider('validMaximumByteLengthProvider')] public function testAcceptsStringsWithinMaximumByteLength(string $value): void { $result = self::stringRecord('com.example.stringLengthNoMinLength', [ 'maxLength' => 4, ]) ->safeParse(self::value('com.example.stringLengthNoMinLength', $value)); static::assertTrue($result->isValid(), \implode('; ', $result->messages())); } /** @return array */ public static function validMaximumByteLengthProvider(): array { return [ 'empty string' => [''], 'one ASCII byte' => ['a'], 'two ASCII bytes' => ['ab'], 'combining acute accent' => ["\u{0301}"], 'ASCII plus combining accent' => ["a\u{0301}"], 'ASCII plus precomposed accent' => ['aé'], 'three ASCII bytes' => ['abc'], 'CJK character' => ['一'], 'four ASCII bytes' => ['abcd'], 'two precomposed accents' => ['éé'], 'ASCII and precomposed accent' => ['aaé'], 'four-byte emoji' => ['👋'], ]; } #[DataProvider('invalidMaximumByteLengthProvider')] public function testRejectsStringsAboveMaximumByteLength(string $value): void { self::assertViolation( self::stringRecord('com.example.stringLengthNoMinLength', ['maxLength' => 4]), self::value('com.example.stringLengthNoMinLength', $value), 'max_length', ); } /** @return array */ public static function invalidMaximumByteLengthProvider(): array { return [ 'five ASCII bytes' => ['abcde'], 'ASCII plus two combining accents' => ["a\u{0301}\u{0301}"], 'three precomposed accents' => ['ééé'], 'emoji plus ASCII' => ['👋a'], 'two emoji' => ['👨👨'], 'family emoji' => ['👨‍👩‍👧‍👧'], ]; } #[DataProvider('validGraphemeLengthProvider')] public function testAcceptsStringsWithinGraphemeConstraints(string $value): void { $result = self::stringRecord('com.example.stringLengthGrapheme', [ 'minGraphemes' => 2, 'maxGraphemes' => 4, ]) ->safeParse(self::value('com.example.stringLengthGrapheme', $value)); static::assertTrue($result->isValid(), \implode('; ', $result->messages())); } /** @return array */ public static function validGraphemeLengthProvider(): array { return [ 'two ASCII graphemes' => ['ab'], 'accented and ASCII graphemes' => ["a\u{0301}b"], 'two accented graphemes' => ["a\u{0301}b\u{0301}"], 'two emoji' => ['😀😀'], 'digits and family emoji' => ['12👨‍👩‍👧‍👧'], 'four ASCII graphemes' => ['abcd'], 'four accented graphemes' => ["a\u{0301}b\u{0301}c\u{0301}d\u{0301}"], ]; } #[DataProvider('invalidGraphemeLengthProvider')] public function testRejectsStringsOutsideGraphemeConstraints(string $value, string $code): void { self::assertViolation( self::stringRecord('com.example.stringLengthGrapheme', [ 'minGraphemes' => 2, 'maxGraphemes' => 4, ]), self::value('com.example.stringLengthGrapheme', $value), $code, ); } /** @return array */ public static function invalidGraphemeLengthProvider(): array { return [ 'empty string' => ['', 'min_graphemes'], 'three combining accents' => ["\u{0301}\u{0301}\u{0301}", 'min_graphemes'], 'one ASCII grapheme' => ['a', 'min_graphemes'], 'base with four combining accents' => ["a\u{0301}\u{0301}\u{0301}\u{0301}", 'min_graphemes'], 'emoji presentation sequence' => ["5\u{FE0F}", 'min_graphemes'], 'family emoji' => ['👨‍👩‍👧‍👧', 'min_graphemes'], 'five ASCII graphemes' => ['abcde', 'max_graphemes'], 'five accented graphemes' => ["a\u{0301}b\u{0301}c\u{0301}d\u{0301}e\u{0301}", 'max_graphemes'], 'five emoji' => ['😀😀😀😀😀', 'max_graphemes'], 'mixed five graphemes' => ['ab😀de', 'max_graphemes'], ]; } public function testAppliesStringEnumConstraint(): void { $schema = self::stringRecord('com.example.stringEnum', ['enum' => ['a', 'b']]); static::assertTrue($schema->safeParse(self::value('com.example.stringEnum', 'a'))->isValid()); self::assertViolation($schema, self::value('com.example.stringEnum', 'c'), 'invalid_enum'); } public function testAppliesStringConstConstraint(): void { $schema = self::stringRecord('com.example.stringConst', ['const' => 'a']); static::assertTrue($schema->safeParse(self::value('com.example.stringConst', 'a'))->isValid()); self::assertViolation($schema, self::value('com.example.stringConst', 'b'), 'invalid_const'); } #[DataProvider('validDatetimeProvider')] public function testAcceptsValidDatetimes(string $value): void { self::assertValidFormat('com.example.datetime', 'datetime', 'datetime', $value); } /** @return array */ public static function validDatetimeProvider(): array { return [ 'milliseconds UTC' => ['2022-12-12T00:50:36.809Z'], 'seconds UTC' => ['2022-12-12T00:50:36Z'], 'one fractional digit UTC' => ['2022-12-12T00:50:36.8Z'], 'two fractional digits UTC' => ['2022-12-12T00:50:36.80Z'], 'zero offset' => ['2022-12-12T00:50:36+00:00'], 'fractional zero offset' => ['2022-12-12T00:50:36.8+00:00'], 'negative offset' => ['2022-12-11T19:50:36-05:00'], 'fractional negative offset' => ['2022-12-11T19:50:36.8-05:00'], 'two fractional digits negative offset' => ['2022-12-11T19:50:36.80-05:00'], 'milliseconds negative offset' => ['2022-12-11T19:50:36.809-05:00'], ]; } public function testRejectsInvalidDatetime(): void { self::assertInvalidFormat('com.example.datetime', 'datetime', 'datetime', 'bad date'); } #[DataProvider('validUriProvider')] public function testAcceptsValidUris(string $value): void { self::assertValidFormat('com.example.uri', 'uri', 'uri', $value); } /** @return array */ public static function validUriProvider(): array { return [ 'HTTPS origin' => ['https://example.com'], 'HTTPS path' => ['https://example.com/with/path'], 'HTTPS query' => ['https://example.com/with/path?and=query'], 'AT URI' => ['at://bsky.social'], 'DID URI' => ['did:example:test'], ]; } public function testRejectsInvalidUri(): void { self::assertInvalidFormat('com.example.uri', 'uri', 'uri', 'not a uri'); } public function testAppliesAtUriFormat(): void { self::assertValidFormat( 'com.example.atUri', 'atUri', 'at-uri', 'at://did:web:example.com/com.example.test/self', ); self::assertInvalidFormat('com.example.atUri', 'atUri', 'at-uri', 'http://not-atproto.com'); } public function testAppliesDidFormat(): void { self::assertValidFormat('com.example.did', 'did', 'did', 'did:web:example.com'); self::assertValidFormat('com.example.did', 'did', 'did', 'did:plc:12345678abcdefghijklmnop'); self::assertInvalidFormat('com.example.did', 'did', 'did', 'bad did'); self::assertInvalidFormat('com.example.did', 'did', 'did', 'did:short'); } public function testAppliesHandleFormat(): void { self::assertValidFormat('com.example.handle', 'handle', 'handle', 'test.bsky.social'); self::assertValidFormat('com.example.handle', 'handle', 'handle', 'bsky.test'); self::assertInvalidFormat('com.example.handle', 'handle', 'handle', 'bad handle'); self::assertInvalidFormat('com.example.handle', 'handle', 'handle', '-bad-.test'); } public function testAppliesAtIdentifierFormat(): void { self::assertValidFormat('com.example.atIdentifier', 'atIdentifier', 'at-identifier', 'bsky.test'); self::assertValidFormat( 'com.example.atIdentifier', 'atIdentifier', 'at-identifier', 'did:plc:12345678abcdefghijklmnop', ); self::assertInvalidFormat('com.example.atIdentifier', 'atIdentifier', 'at-identifier', 'bad id'); self::assertInvalidFormat('com.example.atIdentifier', 'atIdentifier', 'at-identifier', '-bad-.test'); } public function testAppliesNsidFormat(): void { self::assertValidFormat('com.example.nsid', 'nsid', 'nsid', 'com.atproto.test'); self::assertValidFormat('com.example.nsid', 'nsid', 'nsid', 'app.bsky.nested.test'); self::assertInvalidFormat('com.example.nsid', 'nsid', 'nsid', 'bad nsid'); self::assertInvalidFormat('com.example.nsid', 'nsid', 'nsid', 'com.bad-.foo'); } public function testAppliesCidFormat(): void { self::assertValidFormat( 'com.example.cid', 'cid', 'cid', 'bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a', ); self::assertInvalidFormat('com.example.cid', 'cid', 'cid', 'abapsdofiuwrpoiasdfuaspdfoiu'); } public function testAppliesLanguageFormat(): void { self::assertValidFormat('com.example.language', 'language', 'language', 'en-US-boont'); self::assertInvalidFormat('com.example.language', 'language', 'language', 'not-a-language-'); } /** @param array $constraints */ private static function stringRecord(string $id, array $constraints): RecordSchema { $field = \substr($id, \strlen('com.example.')); $field = match ($field) { 'stringLength', 'stringLengthNoMinLength', 'stringLengthGrapheme', 'stringEnum', 'stringConst' => 'string', default => $field, }; return self::record($id, $field, ['type' => 'string'] + $constraints); } /** @param array $definition */ private static function record(string $id, string $field, array $definition): RecordSchema { $result = new LexiconParser()->parse([ 'lexicon' => 1, 'id' => $id, 'defs' => [ 'main' => [ 'type' => 'record', 'key' => 'any', 'record' => [ 'type' => 'object', 'properties' => [$field => $definition], ], ], ], ]); static::assertTrue($result->isValid(), \implode('; ', $result->messages())); static::assertInstanceOf(LexiconDocument::class, $result->value); $repository = new InMemoryLexiconRepository(); $repository->add($result->value); $schema = new SchemaCompiler($repository)->compileRecord($id); static::assertInstanceOf(RecordSchema::class, $schema); return $schema; } private static function assertValidFormat( string $id, string $field, string $format, string $value, ): void { $result = self::record($id, $field, ['type' => 'string', 'format' => $format]) ->safeParse(self::fieldValue($id, $field, $value)); static::assertTrue($result->isValid(), \implode('; ', $result->messages())); } private static function assertInvalidFormat( string $id, string $field, string $format, string $value, ): void { self::assertViolation( self::record($id, $field, ['type' => 'string', 'format' => $format]), self::fieldValue($id, $field, $value), 'invalid_format', '$.' . $field, ); } /** @return array */ private static function value(string $type, string $value): array { return ['$type' => $type, 'string' => $value]; } /** @return array */ private static function fieldValue(string $type, string $field, string $value): array { return ['$type' => $type, $field => $value]; } /** @param array $value */ private static function assertViolation( RecordSchema $schema, array $value, string $code, string $path = '$.string', ): void { $result = $schema->safeParse($value); static::assertFalse($result->isValid()); static::assertContains($path, \array_column($result->violations, 'path')); static::assertContains($code, \array_column($result->violations, 'code')); } }