safeValidate($tokenValue)->isValid()); } #[DataProvider('tokenProvider')] public function testTokenSchemaParsesItsToken(string $definition, string $tokenValue): void { $schema = self::tokenSchema($definition); $result = $schema->safeParse($tokenValue); static::assertTrue($result->isValid()); static::assertSame($tokenValue, $result->value); } #[DataProvider('tokenProvider')] public function testTokenSchemaSerializesToJson(string $definition, string $tokenValue): void { $schema = self::tokenSchema($definition); static::assertSame(\json_encode($tokenValue, JSON_THROW_ON_ERROR), \json_encode($schema, JSON_THROW_ON_ERROR)); } #[DataProvider('tokenProvider')] public function testTokenSchemaStringifiesToItsToken(string $definition, string $tokenValue): void { $schema = self::tokenSchema($definition); static::assertSame($tokenValue, (string) $schema); } /** @return array */ public static function tokenProvider(): array { return [ 'main' => ['main', 'com.example.token'], 'myToken' => ['myToken', 'com.example.token#myToken'], 'anotherToken' => ['anotherToken', 'com.example.token#anotherToken'], ]; } public function testHandlesOpenAndClosedUnions(): void { $schema = self::unionRecord(); $known = [ '$type' => 'com.example.union', 'unionOpen' => [ '$type' => 'com.example.kitchenSink#object', 'object' => ['boolean' => TRUE], 'array' => ['one', 'two'], 'boolean' => TRUE, 'integer' => 123, 'string' => 'string', ], 'unionClosed' => [ '$type' => 'com.example.kitchenSink#subobject', 'boolean' => TRUE, ], ]; $openUnknown = [ '$type' => 'com.example.union', 'unionOpen' => ['$type' => 'com.example.other'], 'unionClosed' => [ '$type' => 'com.example.kitchenSink#subobject', 'boolean' => TRUE, ], ]; static::assertTrue($schema->safeParse($known)->isValid()); static::assertTrue($schema->safeParse($openUnknown)->isValid()); $missingTypes = $schema->safeParse([ '$type' => 'com.example.union', 'unionOpen' => [], 'unionClosed' => [], ]); static::assertFalse($missingTypes->isValid()); static::assertContains('$.unionOpen', \array_column($missingTypes->violations, 'path')); static::assertContains('missing_type', \array_column($missingTypes->violations, 'code')); $closedUnknown = $schema->safeParse([ '$type' => 'com.example.union', 'unionOpen' => ['$type' => 'com.example.other'], 'unionClosed' => [ '$type' => 'com.example.other', 'boolean' => TRUE, ], ]); static::assertFalse($closedUnknown->isValid()); static::assertContains('$.unionClosed', \array_column($closedUnknown->violations, 'path')); static::assertContains('invalid_union_type', \array_column($closedUnknown->violations, 'code')); } public function testHandlesUnknownObjects(): void { $schema = self::record([ 'lexicon' => 1, 'id' => 'com.example.unknown', 'defs' => [ 'main' => [ 'type' => 'record', 'key' => 'tid', 'record' => [ 'type' => 'object', 'required' => ['unknown'], 'properties' => [ 'unknown' => ['type' => 'unknown'], 'optUnknown' => ['type' => 'unknown'], ], ], ], ], ]); static::assertTrue( $schema->safeParse([ '$type' => 'com.example.unknown', 'unknown' => ['foo' => 'bar'], ])->isValid(), ); $missing = $schema->safeParse(['$type' => 'com.example.unknown']); static::assertFalse($missing->isValid()); static::assertContains('$.unknown', \array_column($missing->violations, 'path')); static::assertContains('missing_required', \array_column($missing->violations, 'code')); } public function testCompilerAccessesDefinitionsWithUnsafeCharacters(): void { $compiler = self::unsafeCompiler(); $schema = $compiler->compileRef( 'com.example.4-2.unsafeDefs#ob-je-c$t', 'com.example.4-2.unsafeDefs', ); static::assertInstanceOf(SchemaInterface::class, $schema); $value = [ '$type' => 'com.example.4-2.unsafeDefs#ob-je-c$t', 'foo' => 'bar', ]; $result = $schema->safeParse($value); static::assertTrue($result->isValid(), \implode('; ', $result->messages())); static::assertSame($value, $result->value); } public function testCompilerAccessesDefinitionsNamedWithReservedGeneratedIdentifiers(): void { $compiler = self::unsafeCompiler(); $schema = $compiler->compileRef('com.example.4-2.unsafeDefs#9', 'com.example.4-2.unsafeDefs'); static::assertInstanceOf(SchemaInterface::class, $schema); $result = $schema->safeParse(9); static::assertTrue($result->isValid(), \implode('; ', $result->messages())); static::assertSame(9, $result->value); } private static function tokenSchema(string $definition): TokenSchema { $document = [ 'lexicon' => 1, 'id' => 'com.example.token', 'defs' => [ 'main' => ['type' => 'token', 'description' => 'Main token definition'], 'myToken' => ['type' => 'token', 'description' => 'A custom token'], 'anotherToken' => ['type' => 'token', 'description' => 'Another custom token'], ], ]; $compiler = self::compiler([$document]); $schema = $compiler->compileRef('com.example.token#' . $definition, 'com.example.token'); static::assertInstanceOf(TokenSchema::class, $schema); return $schema; } private static function unionRecord(): RecordSchema { $union = [ 'lexicon' => 1, 'id' => 'com.example.union', 'defs' => [ 'main' => [ 'type' => 'record', 'key' => 'tid', 'record' => [ 'type' => 'object', 'required' => ['unionOpen', 'unionClosed'], 'properties' => [ 'unionOpen' => [ 'type' => 'union', 'refs' => [ 'com.example.kitchenSink#object', 'com.example.kitchenSink#subobject', ], ], 'unionClosed' => [ 'type' => 'union', 'closed' => TRUE, 'refs' => [ 'com.example.kitchenSink#object', 'com.example.kitchenSink#subobject', ], ], ], ], ], ], ]; $compiler = self::compiler([self::kitchenSinkDocument(), $union]); $schema = $compiler->compileRecord('com.example.union'); static::assertInstanceOf(RecordSchema::class, $schema); return $schema; } private static function unsafeCompiler(): SchemaCompiler { return self::compiler([[ 'lexicon' => 1, 'id' => 'com.example.4-2.unsafeDefs', 'defs' => [ 'ob-je-c$t' => [ 'type' => 'object', 'properties' => [], ], 'ff-ri-n.g' => ['type' => 'string'], '9' => ['type' => 'integer', 'enum' => [9]], ], ]]); } /** @param array $document */ private static function record(array $document): RecordSchema { $compiler = self::compiler([$document]); $id = $document['id'] ?? NULL; static::assertIsString($id); $schema = $compiler->compileRecord($id); static::assertInstanceOf(RecordSchema::class, $schema); return $schema; } /** * @param list> $documents */ private static function compiler(array $documents): SchemaCompiler { $repository = new InMemoryLexiconRepository(); foreach ($documents as $raw) { $result = new LexiconParser()->parse($raw); static::assertTrue($result->isValid(), \implode('; ', $result->messages())); static::assertInstanceOf(LexiconDocument::class, $result->value); $repository->add($result->value); } return new SchemaCompiler($repository); } /** @return array */ private static function kitchenSinkDocument(): array { return [ 'lexicon' => 1, 'id' => 'com.example.kitchenSink', 'defs' => [ 'main' => [ 'type' => 'record', 'key' => 'tid', 'record' => [ 'type' => 'object', 'required' => ['object', 'array', 'boolean', 'integer', 'string', 'bytes', 'cidLink'], 'properties' => [ 'object' => ['type' => 'ref', 'ref' => '#object'], 'array' => ['type' => 'array', 'items' => ['type' => 'string']], 'boolean' => ['type' => 'boolean'], 'integer' => ['type' => 'integer'], 'string' => ['type' => 'string'], 'bytes' => ['type' => 'bytes'], 'cidLink' => ['type' => 'cid-link'], ], ], ], 'object' => [ 'type' => 'object', 'required' => ['object', 'array', 'boolean', 'integer', 'string'], 'properties' => [ 'object' => ['type' => 'ref', 'ref' => '#subobject'], 'array' => ['type' => 'array', 'items' => ['type' => 'string']], 'boolean' => ['type' => 'boolean'], 'integer' => ['type' => 'integer'], 'string' => ['type' => 'string'], ], ], 'subobject' => [ 'type' => 'object', 'required' => ['boolean'], 'properties' => [ 'boolean' => ['type' => 'boolean'], ], ], ], ]; } }