[ 'type' => 'array', 'minLength' => 2, 'maxLength' => 4, 'items' => ['type' => 'integer'], ], ]); static::assertTrue($schema->safeParse(self::value('com.example.arrayLength', 'array', [1, 2, 3]))->isValid()); self::assertViolation($schema, self::value('com.example.arrayLength', 'array', [1]), '$.array', 'min_length'); self::assertViolation( $schema, self::value('com.example.arrayLength', 'array', [1, 2, 3, 4, 5]), '$.array', 'max_length', ); } public function testAppliesArrayItemConstraints(): void { $schema = self::record('com.example.arrayLength', [ 'array' => [ 'type' => 'array', 'minLength' => 2, 'maxLength' => 4, 'items' => ['type' => 'integer'], ], ]); self::assertViolation( $schema, self::value('com.example.arrayLength', 'array', [1, '2', 3]), '$.array[1]', 'invalid_type', ); self::assertViolation( $schema, self::value('com.example.arrayLength', 'array', [1, NULL, 3]), '$.array[1]', 'invalid_type', ); } public function testAppliesBooleanConstConstraint(): void { $schema = self::record('com.example.boolConst', [ 'boolean' => ['type' => 'boolean', 'const' => FALSE], ]); static::assertTrue($schema->safeParse(self::value('com.example.boolConst', 'boolean', FALSE))->isValid()); self::assertViolation( $schema, self::value('com.example.boolConst', 'boolean', TRUE), '$.boolean', 'invalid_const', ); self::assertViolation( $schema, self::value('com.example.boolConst', 'boolean', 'true'), '$.boolean', 'invalid_type', ); self::assertViolation( $schema, self::value('com.example.boolConst', 'boolean', 1), '$.boolean', 'invalid_type', ); } public function testAppliesBytesLengthConstraints(): void { $schema = self::record('com.example.byteLength', [ 'bytes' => ['type' => 'bytes', 'minLength' => 2, 'maxLength' => 4], ]); static::assertTrue( $schema->safeParse(self::value('com.example.byteLength', 'bytes', ['$bytes' => 'AQID']))->isValid(), ); self::assertViolation( $schema, self::value('com.example.byteLength', 'bytes', ['$bytes' => 'AQ==']), '$.bytes', 'min_length', ); self::assertViolation( $schema, self::value('com.example.byteLength', 'bytes', ['$bytes' => 'AQIDBAU=']), '$.bytes', 'max_length', ); } public function testGeneratesDefaultProperties(): void { $document = [ 'lexicon' => 1, 'id' => 'com.example.default', 'defs' => [ 'main' => [ 'type' => 'record', 'key' => 'any', 'record' => [ 'type' => 'object', 'required' => ['boolean'], 'properties' => [ 'boolean' => ['type' => 'boolean', 'default' => FALSE], 'integer' => ['type' => 'integer', 'default' => 0], 'string' => ['type' => 'string', 'default' => ''], 'object' => ['type' => 'ref', 'ref' => '#object'], ], ], ], 'object' => [ 'type' => 'object', 'properties' => [ 'boolean' => ['type' => 'boolean', 'default' => TRUE], 'integer' => ['type' => 'integer', 'default' => 1], 'string' => ['type' => 'string', 'default' => 'x'], ], ], ], ]; $schema = self::compiledRecord($document); $result = $schema->safeParse([ '$type' => 'com.example.default', 'object' => [], ]); static::assertTrue($result->isValid(), \implode('; ', $result->messages())); static::assertIsArray($result->value); static::assertEquals( [ '$type' => 'com.example.default', 'boolean' => FALSE, 'integer' => 0, 'string' => '', 'object' => [ 'boolean' => TRUE, 'integer' => 1, 'string' => 'x', ], ], $result->value, ); static::assertArrayNotHasKey('datetime', $result->value); } public function testPrimitiveAndOptionalDefaultsRespectParseMode(): void { $integer = new IntegerSchema(); static::assertFalse($integer->safeParse(NULL)->isValid()); static::assertFalse($integer->safeValidate(NULL)->isValid()); $optionalInteger = new ObjectSchema(['value' => $integer]); static::assertSame([], $optionalInteger->safeParse([])->value); static::assertSame([], $optionalInteger->safeValidate([])->value); $defaultInteger = new IntegerSchema(default: 42); $parsedDefault = $defaultInteger->safeParse(NULL); static::assertTrue($parsedDefault->isValid()); static::assertSame(42, $parsedDefault->value); static::assertFalse($defaultInteger->safeValidate(NULL)->isValid()); $optionalDefault = new ObjectSchema(['value' => $defaultInteger]); static::assertSame(['value' => 42], $optionalDefault->safeParse([])->value); static::assertSame([], $optionalDefault->safeValidate([])->value); $object = new ObjectSchema([ 'foo' => new \Wotnak\Atproto\Lexicon\Schema\StringSchema(default: 'aze'), ]); static::assertSame(['foo' => 'aze'], $object->safeParse([])->value); static::assertSame([], $object->safeValidate([])->value); } public function testAppliesIntegerRangeConstraint(): void { $schema = self::record('com.example.integerRange', [ 'integer' => ['type' => 'integer', 'minimum' => 2, 'maximum' => 4], ]); static::assertTrue($schema->safeParse(self::value('com.example.integerRange', 'integer', 2))->isValid()); self::assertViolation( $schema, self::value('com.example.integerRange', 'integer', 1), '$.integer', 'minimum', ); self::assertViolation( $schema, self::value('com.example.integerRange', 'integer', 5), '$.integer', 'maximum', ); } public function testAppliesIntegerEnumConstraint(): void { $schema = self::record('com.example.integerEnum', [ 'integer' => ['type' => 'integer', 'enum' => [1, 2]], ]); static::assertTrue($schema->safeParse(self::value('com.example.integerEnum', 'integer', 2))->isValid()); self::assertViolation( $schema, self::value('com.example.integerEnum', 'integer', 0), '$.integer', 'invalid_enum', ); } public function testAppliesIntegerConstConstraint(): void { $schema = self::record('com.example.integerConst', [ 'integer' => ['type' => 'integer', 'const' => 0], ]); static::assertTrue($schema->safeParse(self::value('com.example.integerConst', 'integer', 0))->isValid()); self::assertViolation( $schema, self::value('com.example.integerConst', 'integer', 1), '$.integer', 'invalid_const', ); } public function testRejectsNonWholeNumbersAsIntegers(): void { $schema = self::record('com.example.integerRange', [ 'integer' => ['type' => 'integer', 'minimum' => 2, 'maximum' => 4], ]); self::assertViolation( $schema, self::value('com.example.integerRange', 'integer', 2.5), '$.integer', 'invalid_type', ); } /** * @param array $properties * @param list $required */ private static function record(string $id, array $properties, array $required = []): RecordSchema { return self::compiledRecord([ 'lexicon' => 1, 'id' => $id, 'defs' => [ 'main' => [ 'type' => 'record', 'key' => 'any', 'record' => [ 'type' => 'object', 'required' => $required, 'properties' => $properties, ], ], ], ]); } /** @param array $document */ private static function compiledRecord(array $document): RecordSchema { $result = new LexiconParser()->parse($document); 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($result->value->id); static::assertInstanceOf(RecordSchema::class, $schema); return $schema; } /** @return array */ private static function value(string $type, string $field, mixed $value): array { return ['$type' => $type, $field => $value]; } /** @param array $value */ private static function assertViolation( RecordSchema $schema, array $value, string $path, string $code, ): 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')); } }