$value */ #[DataProvider('validParametersProvider')] public function testAcceptsParameterValues(array $value): void { static::assertTrue( new ParamsSchema([]) ->safeValidate($value) ->isValid(), ); } /** * @return array}> */ public static function validParametersProvider(): array { return [ 'empty object' => [[]], 'string' => [['name' => 'Alice']], 'empty string' => [['name' => '']], 'integer' => [['age' => 30]], 'negative integer' => [['offset' => -5]], 'boolean' => [['active' => TRUE]], 'empty array' => [['tags' => []]], 'string array' => [['tags' => ['one', 'two']]], 'integer array' => [['ids' => [1, 2, 3]]], 'boolean array' => [['flags' => [TRUE, FALSE]]], ]; } #[DataProvider('invalidParametersProvider')] public function testRejectsInvalidParameterValues(mixed $value, string $path): void { $result = new ParamsSchema([])->safeValidate($value); static::assertFalse($result->isValid()); static::assertSame([$path], \array_column($result->violations, 'path')); } /** * @return array */ public static function invalidParametersProvider(): array { return [ 'null' => [NULL, '$'], 'string' => ['name=Alice', '$'], 'list' => [['Alice'], '$'], 'null value' => [['value' => NULL], '$.value'], 'float' => [['value' => 3.14], '$.value'], 'object' => [['value' => ['nested' => 'text']], '$.value'], 'mixed scalar array' => [['value' => ['one', 2]], '$.value'], 'nested array' => [['value' => [['nested']]], '$.value'], ]; } public function testValidatesDeclaredParameters(): void { $schema = new ParamsSchema( [ 'name' => new StringSchema(), 'age' => new IntegerSchema(minimum: 0), 'active' => new BooleanSchema(), 'tags' => new ArraySchema(new StringSchema()), ], ['name'], ); $valid = $schema->safeValidate([ 'name' => 'Alice', 'age' => 30, 'active' => TRUE, 'tags' => ['one', 'two'], ]); $invalid = $schema->safeValidate([ 'age' => -1, 'active' => 'true', 'tags' => ['one', 2], ]); static::assertTrue($valid->isValid()); static::assertFalse($invalid->isValid()); static::assertSame( ['$.name', '$.age', '$.active', '$.tags[1]'], \array_column($invalid->violations, 'path'), ); } public function testSafeParseAppliesDeclaredDefaults(): void { $schema = new ParamsSchema([ 'limit' => new IntegerSchema(default: 50), 'reverse' => new BooleanSchema(default: FALSE), ]); $result = $schema->safeParse([]); static::assertTrue($result->isValid()); static::assertSame(['limit' => 50, 'reverse' => FALSE], $result->value); } public function testSafeParseAppliesDefaultsBeforeCheckingRequiredParameters(): void { $schema = new ParamsSchema( ['limit' => new IntegerSchema(default: 50)], ['limit'], ); $parsed = $schema->safeParse([]); $validated = $schema->safeValidate([]); static::assertTrue($parsed->isValid()); static::assertSame(['limit' => 50], $parsed->value); static::assertFalse($validated->isValid()); static::assertSame(['missing_required'], \array_column($validated->violations, 'code')); } public function testSafeParseValidatesInjectedDefaultsAgainstConstraints(): void { $schema = new ParamsSchema([ 'name' => new StringSchema(minLength: 5, default: 'bad'), ]); $result = $schema->safeParse([]); static::assertFalse($result->isValid()); static::assertSame(['$.name'], \array_column($result->violations, 'path')); static::assertSame(['min_length'], \array_column($result->violations, 'code')); } }