safeValidate($value) ->isValid(), ); } /** @return array */ public static function nonStringProvider(): array { return [ 'null' => [NULL], 'integer' => [123], 'boolean' => [TRUE], 'list' => [['hello']], 'object' => [['value' => 'hello']], ]; } public function testByteLengthConstraintsIncludeUtf8Bytes(): void { $schema = new StringSchema(minLength: 3, maxLength: 10); static::assertTrue($schema->safeValidate('abc')->isValid()); static::assertTrue($schema->safeValidate('1234567890')->isValid()); static::assertFalse($schema->safeValidate('hi')->isValid()); static::assertFalse($schema->safeValidate('hello world!')->isValid()); static::assertTrue( new StringSchema(maxLength: 4) ->safeValidate('๐Ÿ˜€') ->isValid(), ); static::assertFalse( new StringSchema(maxLength: 3) ->safeValidate('๐Ÿ˜€') ->isValid(), ); } public function testCombinedGraphemeConstraints(): void { $schema = new StringSchema(minGraphemes: 2, maxGraphemes: 5); static::assertTrue($schema->safeValidate('hi')->isValid()); static::assertTrue($schema->safeValidate('hello')->isValid()); static::assertFalse($schema->safeValidate('a')->isValid()); static::assertFalse($schema->safeValidate('hello!')->isValid()); static::assertTrue( new StringSchema(minGraphemes: 3) ->safeValidate('๐Ÿ˜€๐Ÿ˜€๐Ÿ˜€') ->isValid(), ); } public function testDefaultEnumAndConstConstraints(): void { $default = new StringSchema(default: 'default value'); static::assertSame('default value', $default->safeParse(NULL)->value); static::assertFalse($default->safeValidate(NULL)->isValid()); $invalidDefault = new StringSchema(minLength: 5, default: 'hi'); static::assertFalse($invalidDefault->safeParse(NULL)->isValid()); $enum = new StringSchema(enum: ['active', 'inactive']); static::assertTrue($enum->safeValidate('active')->isValid()); static::assertFalse($enum->safeValidate('unknown')->isValid()); $const = new StringSchema(const: 'self'); static::assertTrue($const->safeValidate('self')->isValid()); static::assertFalse($const->safeValidate('other')->isValid()); } public function testStringsMustContainValidUtf8(): void { $schemaResult = new StringSchema()->safeValidate("\xFF"); static::assertFalse($schemaResult->isValid()); static::assertSame(['invalid_utf8'], \array_column($schemaResult->violations, 'code')); $recordResult = new RecordValidator()->validateRecord( 'com.example.post', [ '$type' => 'com.example.post', 'text' => 'Hello', 'futureField' => "\xFF", ], self::recordSchema(), ); static::assertFalse($recordResult->isValid()); static::assertSame(['$.futureField'], \array_column($recordResult->violations, 'path')); static::assertSame(['invalid_utf8'], \array_column($recordResult->violations, 'code')); $invalidKeyResult = new RecordValidator()->validateRecord( 'com.example.post', [ '$type' => 'com.example.post', 'text' => 'Hello', "\xFF" => TRUE, ], self::recordSchema(), ); static::assertFalse($invalidKeyResult->isValid()); static::assertSame(['invalid_lex_key'], \array_column($invalidKeyResult->violations, 'code')); } public function testUriFormatAcceptsNonHttpUrisAndRejectsMalformedEscapes(): void { $schema = new StringSchema(format: 'uri'); static::assertTrue($schema->safeValidate('did:plc:abc123')->isValid()); static::assertTrue($schema->safeValidate('urn:ietf:params:oauth:request_uri:abc')->isValid()); static::assertTrue($schema->safeValidate('https://[2001:db8::1]/path')->isValid()); static::assertFalse($schema->safeValidate('https://example.com/%zz')->isValid()); static::assertFalse($schema->safeValidate('https://example.com:abc')->isValid()); static::assertFalse($schema->safeValidate('foo:[bar]')->isValid()); static::assertFalse($schema->safeValidate('not a uri')->isValid()); } public function testUnsupportedFormatNeverSilentlyAcceptsValues(): void { $schema = new StringSchema(format: 'datatime'); static::assertFalse($schema->safeValidate('anything')->isValid()); } #[DataProvider('languageTagProvider')] public function testLanguageFormatFollowsBcp47(string $tag, bool $valid): void { $schema = new StringSchema(format: 'language'); static::assertSame($valid, $schema->safeValidate($tag)->isValid(), $tag); } /** * @return array */ public static function languageTagProvider(): array { return [ 'language' => ['en', TRUE], 'region' => ['en-US', TRUE], 'script and region' => ['zh-Hant-CN', TRUE], 'extension' => ['en-u-ca-gregory', TRUE], 'private use' => ['x-private', TRUE], 'grandfathered' => ['i-klingon', TRUE], 'one-character extension value' => ['en-a', FALSE], 'one-character language' => ['a-value', FALSE], 'empty subtag' => ['en--US', FALSE], ]; } public function testStrictDatetimeValidatesCalendarAndTimeComponents(): void { $schema = new StringSchema(format: 'datetime'); static::assertTrue($schema->safeValidate('2024-02-29T23:59:59.123Z')->isValid()); static::assertTrue($schema->safeValidate('0000-01-01T00:00:00.000Z')->isValid()); static::assertFalse($schema->safeValidate('2023-02-29T12:00:00Z')->isValid()); static::assertFalse($schema->safeValidate('2024-02-29 23:59:59Z')->isValid()); static::assertFalse($schema->safeValidate('2024-02-29T23:59:59z')->isValid()); static::assertFalse($schema->safeValidate('2024-01-01T24:00:00Z')->isValid()); static::assertFalse($schema->safeValidate('2024-01-01T12:00:00+24:00')->isValid()); static::assertFalse($schema->safeValidate('2024-01-01T12:00:00-00:00')->isValid()); static::assertFalse($schema->safeValidate('0000-01-01T00:00:00+01:00')->isValid()); } public function testLooseDatetimeAllowsCommonNonCanonicalForms(): void { $schema = new StringSchema(format: 'datetime'); $loose = ValidationOptions::validate(FALSE); static::assertTrue($schema->safeValidate('2023-12-25T12:00:00', $loose)->isValid()); static::assertTrue($schema->safeValidate('2023-12-25', $loose)->isValid()); static::assertTrue($schema->safeValidate('2023-12-25T12:00:00+05:30', $loose)->isValid()); } #[DataProvider('formatProvider')] public function testLexiconStringFormats(string $format, string $value, bool $valid): void { static::assertSame( $valid, new StringSchema(format: $format) ->safeValidate($value) ->isValid(), ); } /** @return array */ public static function formatProvider(): array { return [ 'AT URI' => ['at-uri', 'at://did:plc:abc123/app.bsky.feed.post/xyz', TRUE], 'AT URI rejects HTTP' => ['at-uri', 'https://example.com', FALSE], 'PLC DID' => ['did', 'did:plc:abc123', TRUE], 'web DID' => ['did', 'did:web:example.com', TRUE], 'invalid DID' => ['did', 'did:', FALSE], 'handle' => ['handle', 'user.bsky.social', TRUE], 'subdomain handle' => ['handle', 'alice.test.example.com', TRUE], 'invalid handle' => ['handle', 'invalid handle!', FALSE], 'AT identifier DID' => ['at-identifier', 'did:plc:abc123', TRUE], 'AT identifier handle' => ['at-identifier', 'user.bsky.social', TRUE], 'invalid AT identifier' => ['at-identifier', 'invalid!', FALSE], 'NSID' => ['nsid', 'app.bsky.feed.post', TRUE], 'long NSID' => ['nsid', 'com.example.app.feature.action', TRUE], 'invalid NSID' => ['nsid', 'app.bsky.feed!', FALSE], 'CID' => ['cid', 'bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi', TRUE], 'invalid CID' => ['cid', 'not-a-cid', FALSE], 'TID' => ['tid', '3jzfcijpj2z2a', TRUE], 'invalid TID' => ['tid', '3jzfcijpj2z2!', FALSE], 'record key TID' => ['record-key', '3jzfcijpj2z2a', TRUE], 'record key text' => ['record-key', 'myRecordKey123', TRUE], 'record key slash' => ['record-key', 'invalid/key', FALSE], 'record key space' => ['record-key', 'invalid key', FALSE], ]; } public function testStrictLanguageTagsRejectDuplicateVariantsAndSingletons(): void { $schema = new StringSchema(format: 'language'); static::assertFalse($schema->safeValidate('de-DE-1901-1901')->isValid()); static::assertFalse($schema->safeValidate('en-a-foo-a-bar')->isValid()); static::assertTrue( $schema ->safeValidate( 'de-DE-1901-1901', ValidationOptions::validate(FALSE), ) ->isValid(), ); static::assertTrue( $schema ->safeValidate( 'en-a-foo-a-bar', ValidationOptions::validate(FALSE), ) ->isValid(), ); } public function testGraphemeLimitsCountExtendedGraphemeClusters(): void { $schema = new StringSchema(maxGraphemes: 1); static::assertTrue($schema->safeValidate('๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ')->isValid()); static::assertFalse($schema->safeValidate('ab')->isValid()); } #[DataProvider('utf8ByteLengthVectorProvider')] public function testByteLengthMatchesUtf8Vectors( string $implementation, string $value, int $length, ): void { static::assertNotSame('', $implementation); static::assertTrue( new StringSchema(minLength: $length, maxLength: $length) ->safeValidate($value) ->isValid(), ); static::assertFalse( new StringSchema(maxLength: $length - 1) ->safeValidate($value) ->isValid(), ); } /** @return iterable */ public static function utf8ByteLengthVectorProvider(): iterable { $values = [ 'ASCII letter' => ['a', 1], 'ASCII symbol' => ['~', 1], 'o diaeresis' => ['รถ', 2], 'n tilde' => ['รฑ', 2], 'copyright' => ['ยฉ', 2], 'CJK radical' => ['โฝ˜', 3], 'telephone' => ['โ˜Ž', 3], 'Egyptian hieroglyph' => ['๐“‹“', 4], 'emoji' => ['๐Ÿ˜€', 4], 'family emoji' => ['๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ง', 25], ]; foreach (['node', 'compute'] as $implementation) { foreach ($values as $name => [$value, $length]) { yield $implementation . ' ' . $name => [$implementation, $value, $length]; } } } #[DataProvider('graphemeLengthVectorProvider')] public function testGraphemeLengthMatchesVectors( string $implementation, string $value, int $length, ): void { static::assertNotSame('', $implementation); static::assertTrue( new StringSchema(minGraphemes: $length, maxGraphemes: $length) ->safeValidate($value) ->isValid(), ); static::assertFalse( new StringSchema(maxGraphemes: $length - 1) ->safeValidate($value) ->isValid(), ); } /** @return iterable */ public static function graphemeLengthVectorProvider(): iterable { $values = [ 'ASCII letter' => ['a', 1], 'ASCII symbol' => ['~', 1], 'o diaeresis' => ['รถ', 1], 'n tilde' => ['รฑ', 1], 'copyright' => ['ยฉ', 1], 'CJK radical' => ['โฝ˜', 1], 'telephone' => ['โ˜Ž', 1], 'Egyptian hieroglyph' => ['๐“‹“', 1], 'emoji' => ['๐Ÿ˜€', 1], 'family emoji' => ['๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ง', 1], 'combined string' => ['a~รถรฑยฉโฝ˜โ˜Ž๐“‹“๐Ÿ˜€๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ง', 10], 'Hindi issue 4321' => ['เคจเคฎเคธเฅเคคเฅ‡', 3], ]; foreach (['native', 'ponyfill'] as $implementation) { foreach ($values as $name => [$value, $length]) { yield $implementation . ' ' . $name => [$implementation, $value, $length]; } } } /** * @return array */ private static function recordSchema(): array { return [ 'type' => 'object', 'required' => ['text'], 'properties' => [ 'text' => ['type' => 'string'], ], ]; } }