'com.example.kitchenSink', 'object' => [ 'object' => ['boolean' => TRUE], 'array' => ['one', 'two'], 'boolean' => TRUE, 'integer' => 123, 'string' => 'string', 'refToEnumWithDefault' => 'option3', ], 'array' => ['one', 'two'], 'boolean' => TRUE, 'integer' => 123, 'string' => 'string', 'datetime' => '2026-08-29T12:00:00.000Z', 'atUri' => 'at://did:web:example.com/com.example.test/self', 'did' => 'did:web:example.com', 'cid' => 'bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a', 'bytes' => ['$bytes' => 'AAECAw=='], 'cidLink' => ['$link' => 'bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a'], ]; $result = $schema->safeParse($value); static::assertTrue($result->isValid(), \implode('; ', $result->messages())); static::assertSame($value, $result->value); } public function testValidatesObjectsCorrectlyAndAppliesDefaultsThroughRefs(): void { $schema = self::schema('com.example.kitchenSink#object'); static::assertInstanceOf(ObjectSchema::class, $schema); $value = self::validObject(); $result = $schema->safeParse($value); static::assertTrue($result->isValid(), \implode('; ', $result->messages())); static::assertSame($value + ['refToEnumWithDefault' => 'option3'], $result->value); } public function testRejectsInvalidEnumValues(): void { $schema = self::schema('com.example.kitchenSink#object'); static::assertInstanceOf(ObjectSchema::class, $schema); $value = self::validObject() + ['refToEnumWithDefault' => 'invalidOption']; $result = $schema->safeParse($value); static::assertFalse($result->isValid()); static::assertSame(['invalid_enum'], \array_column($result->violations, 'code')); static::assertSame(['$.refToEnumWithDefault'], \array_column($result->violations, 'path')); } public function testDoesNotApplyDefaultsWhenValidating(): void { $schema = self::schema('com.example.kitchenSink#object'); static::assertInstanceOf(ObjectSchema::class, $schema); $value = self::validObject(); $result = $schema->safeValidate($value); static::assertTrue($result->isValid(), \implode('; ', $result->messages())); static::assertSame($value, $result->value); } public function testAllowsMissingOptionalRecordFields(): void { $schema = self::schema('com.example.kitchenSink#object'); static::assertInstanceOf(ObjectSchema::class, $schema); $result = $schema->safeValidate(self::validObject()); static::assertTrue($result->isValid(), \implode('; ', $result->messages())); } public function testRejectsMissingRequiredRecordFields(): void { $schema = self::schema('com.example.kitchenSink#object'); static::assertInstanceOf(ObjectSchema::class, $schema); $value = self::validObject(); unset($value['array']); $result = $schema->safeParse($value); static::assertFalse($result->isValid()); static::assertSame(['missing_required'], \array_column($result->violations, 'code')); static::assertSame(['$.array'], \array_column($result->violations, 'path')); } public function testRejectsReferencesWithMultipleHashSegments(): void { $result = new LexiconParser()->parse([ 'lexicon' => 1, 'id' => 'com.example.invalid', 'defs' => [ 'main' => [ 'type' => 'object', 'properties' => [ 'test' => ['type' => 'ref', 'ref' => 'com.example.invalid#test#test'], ], ], ], ]); static::assertFalse($result->isValid()); static::assertContains('invalid_ref', \array_column($result->violations, 'code')); static::assertContains('$.defs.main.properties.test.ref', \array_column($result->violations, 'path')); } public function testReportsMissingExternalLexiconWhenResolvingReference(): void { $document = new LexiconDocument( 'com.example.invalid', [ 'main' => [ 'type' => 'object', 'properties' => [ 'test' => ['type' => 'ref', 'ref' => 'com.example.missing#main'], ], ], ], [], ); $repository = new InMemoryLexiconRepository(); $repository->add($document); $schema = new SchemaCompiler($repository)->compileRef('com.example.invalid', 'com.example.invalid'); static::assertInstanceOf(ObjectSchema::class, $schema); $result = $schema->safeValidate(['test' => []]); static::assertFalse($result->isValid()); static::assertSame(['unresolved_ref'], \array_column($result->violations, 'code')); static::assertSame(['$.test'], \array_column($result->violations, 'path')); } /** @param array $schema */ #[DataProvider('incompleteSchemaProvider')] public function testCompilerRejectsIncompleteSchemas(array $schema, string $message): void { $document = new LexiconDocument('com.example.invalid', ['main' => ['type' => 'integer']], []); $compiler = new SchemaCompiler(new InMemoryLexiconRepository()); $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage($message); $compiler->compileSchema($document, $schema); } /** * @return array, string}> */ public static function incompleteSchemaProvider(): array { return [ 'array items' => [['type' => 'array'], 'item schema'], 'reference' => [['type' => 'ref'], 'reference'], ]; } public function testCompilerRejectsIncompleteRecordDefinition(): void { $document = new LexiconDocument('com.example.invalid', ['main' => ['type' => 'record']], []); $repository = new InMemoryLexiconRepository(); $repository->add($document); $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('record schema'); new SchemaCompiler($repository)->compileRef('com.example.invalid', 'com.example.invalid'); } public function testRejectsMissingLocalDefinitionWhenResolvingReference(): void { $result = new LexiconParser()->parse([ 'lexicon' => 1, 'id' => 'com.example.invalid', 'defs' => [ 'main' => [ 'type' => 'object', 'properties' => [ 'test' => ['type' => 'ref', 'ref' => 'com.example.invalid#nonexistent'], ], ], ], ]); static::assertFalse($result->isValid()); static::assertContains('invalid_ref_target', \array_column($result->violations, 'code')); static::assertContains('$.defs.main.properties.test.ref', \array_column($result->violations, 'path')); } private static function schema(string $ref): SchemaInterface { $result = new LexiconParser()->parse(self::kitchenSinkDocument()); static::assertTrue($result->isValid(), \implode('; ', $result->messages())); static::assertInstanceOf(LexiconDocument::class, $result->value); $repository = new InMemoryLexiconRepository(); $repository->add($result->value); $schema = new SchemaCompiler($repository)->compileRef($ref, 'com.example.kitchenSink'); static::assertInstanceOf(SchemaInterface::class, $schema); return $schema; } /** * @return array */ private static function validObject(): array { return [ 'object' => ['boolean' => TRUE], 'array' => ['one', 'two'], 'boolean' => TRUE, 'integer' => 123, 'string' => 'string', ]; } /** * @return array */ private static function kitchenSinkDocument(): array { return [ 'lexicon' => 1, 'id' => 'com.example.kitchenSink', 'defs' => [ 'main' => [ 'type' => 'record', 'description' => 'A 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'], 'refToEnumWithDefault' => ['type' => 'ref', 'ref' => '#enumWithDefault'], ], ], 'subObject' => [ 'type' => 'object', 'required' => ['boolean'], 'properties' => [ 'boolean' => ['type' => 'boolean'], ], ], 'enumWithDefault' => [ 'type' => 'string', 'default' => 'option3', 'enum' => ['option1', 'option2', 'option3'], ], ], ]; } }