safeValidate([ 'name' => 'Alice', 'age' => 30, 'gender' => 'female', ])->isValid(), ); static::assertTrue($schema->safeValidate(['name' => 'Alice'])->isValid()); static::assertTrue( $schema->safeValidate([ 'name' => 'Alice', 'gender' => NULL, ])->isValid(), ); static::assertTrue( $schema->safeValidate([ 'name' => 'Alice', 'age' => 30, 'extra' => 'value', ])->isValid(), ); } public function testReportsMissingAndInvalidProperties(): void { $missing = self::personSchema() ->safeValidate([ 'age' => 30, 'gender' => 'female', ]); static::assertFalse($missing->isValid()); static::assertSame(['$.name'], \array_column($missing->violations, 'path')); $invalid = self::personSchema() ->safeValidate([ 'name' => 'Alice', 'age' => 'thirty', 'gender' => 'unknown', ]); static::assertFalse($invalid->isValid()); static::assertSame(['$.age', '$.gender'], \array_column($invalid->violations, 'path')); } #[DataProvider('nonObjectProvider')] public function testRejectsNonObjects(mixed $value): void { static::assertFalse(self::personSchema()->safeValidate($value)->isValid()); } /** @return array */ public static function nonObjectProvider(): array { return [ 'null' => [NULL], 'string' => ['not an object'], 'integer' => [42], 'boolean' => [TRUE], 'empty list' => [[]], 'list' => [['Alice', 30]], ]; } public function testAcceptsNativeEmptyJsonObject(): void { $value = new \stdClass(); $result = new ObjectSchema([])->safeValidate($value); static::assertTrue($result->isValid()); static::assertEquals($value, $result->value); static::assertSame('{}', \json_encode($result->value, JSON_THROW_ON_ERROR)); } /** * @param array $value */ #[DataProvider('compoundObjectProvider')] public function testRejectsCompoundDataAsAnObject(array $value): void { $result = new ObjectSchema([])->safeValidate($value); static::assertFalse($result->isValid()); static::assertSame(['invalid_type'], \array_column($result->violations, 'code')); } /** * @return array}> */ public static function compoundObjectProvider(): array { return [ 'CID link' => [[ '$link' => 'bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a', ]], 'bytes' => [['$bytes' => 'SGVsbG8=']], 'blob' => [[ '$type' => 'blob', 'ref' => ['$link' => 'bafkreig77vqcdozl2wyk6z3cscaj5q5fggi53aoh64fewkdiri3cdauyn4'], 'mimeType' => 'image/jpeg', 'size' => 10_000, ]], ]; } public function testSafeParseAppliesDefaultsToOmittedFieldsIncludingRequiredFields(): void { $schema = new ObjectSchema( properties: [ 'name' => new StringSchema(default: 'fallback'), 'enabled' => new BooleanSchema(default: FALSE), 'count' => new IntegerSchema(default: 0), 'nullable' => new StringSchema(default: 'not-used'), 'required' => new StringSchema(default: 'not-used'), ], required: ['required'], nullable: ['nullable'], ); $result = $schema->safeParse(['nullable' => NULL]); static::assertTrue($result->isValid()); static::assertSame( [ 'nullable' => NULL, 'name' => 'fallback', 'enabled' => FALSE, 'count' => 0, 'required' => 'not-used', ], $result->value, ); $validated = $schema->safeValidate(['nullable' => NULL]); static::assertFalse($validated->isValid()); static::assertSame(['$.required'], \array_column($validated->violations, 'path')); } public function testSafeParseValidatesInjectedDefaultsAgainstConstraints(): void { $schema = new ObjectSchema([ 'name' => new StringSchema(minLength: 5, default: 'bad'), ]); $result = $schema->safeParse(new \stdClass()); static::assertFalse($result->isValid()); static::assertSame(['$.name'], \array_column($result->violations, 'path')); static::assertSame(['min_length'], \array_column($result->violations, 'code')); } public function testUnknownPropertiesArePreservedWithoutSchemaValidation(): void { $value = [ 'nested' => ['score' => 1.5], ]; $result = new ObjectSchema([])->safeParse($value); static::assertTrue($result->isValid()); static::assertSame($value, $result->value); } private static function personSchema(): ObjectSchema { return new ObjectSchema( properties: [ 'name' => new StringSchema(), 'age' => new IntegerSchema(), 'gender' => new StringSchema(enum: ['male', 'female']), ], required: ['name'], nullable: ['gender'], ); } }