assoconnect/graphql-mutation-validator-bundle
Install the Package Add the package via Composer:
composer require assoconnect/graphql-mutation-validator-bundle
Register the bundle in config/bundles.php (if using Symfony) or ensure it’s auto-discovered in Laravel.
Extend the Base Validator
Create a custom validator class extending GraphQLMutationValidator:
use Assoconnect\GraphQLMutationValidatorBundle\Validator\GraphQLMutationValidator;
class UserCreateValidator extends GraphQLMutationValidator
{
protected function getRules(): array
{
return [
'name' => 'required|string|max:255',
'email' => 'required|email|unique:users',
'password' => 'required|string|min:8',
];
}
}
Bind the Validator to a Mutation In your GraphQL schema resolver, inject the validator:
use Assoconnect\GraphQLMutationValidatorBundle\Validator\GraphQLMutationValidatorInterface;
class UserCreateResolver
{
public function __construct(
private GraphQLMutationValidatorInterface $validator
) {}
public function resolve($root, array $args)
{
$validator = new UserCreateValidator();
$errors = $validator->validate($args['input']);
if ($errors) {
throw new ValidationException($errors);
}
// Proceed with mutation logic
}
}
First Use Case: Form Validation
Use this for validating GraphQL mutations like createUser, updateProfile, etc. The validator handles:
required, email).laravel-bundle).Define Rules
Override getRules() in your custom validator to specify rules per field:
protected function getRules(): array
{
return [
'username' => 'required|unique:users|alpha_dash',
'age' => 'nullable|integer|min:18',
];
}
Custom Error Messages
Override getMessages() for localized or custom feedback:
protected function getMessages(): array
{
return [
'username.required' => 'A username is required.',
'age.min' => 'You must be at least 18 years old.',
];
}
Dynamic Rules Use closures for dynamic validation (e.g., conditional rules):
protected function getRules(): array
{
return [
'password' => function ($attribute, $value, $fail) {
if (strlen($value) < 8) {
$fail('Password must be at least 8 characters.');
}
},
];
}
Integration with Laravel
If using the laravel-bundle, leverage Laravel’s validation helpers:
use Illuminate\Support\Facades\Validator;
protected function getRules(): array
{
return [
'email' => ['required', 'email', Rule::unique('users')->ignore($this->userId)],
];
}
Reusable Validators
Create base validators for shared logic (e.g., BaseUserValidator) and extend them:
class AdminUserValidator extends BaseUserValidator
{
protected function getRules(): array
{
return array_merge(parent::getRules(), [
'role' => 'required|in:admin,superadmin',
]);
}
}
GraphQL Schema Alignment Ensure your validator fields match the GraphQL input type:
input UserInput {
name: String!
email: String!
}
// Validator rules must include 'name' and 'email'.
Error Handling
Catch ValidationException in your resolver and return GraphQL errors:
try {
$this->validator->validate($input);
} catch (ValidationException $e) {
return new ErrorResult($e->errors());
}
Testing Mock validators in tests:
$validator = $this->createMock(GraphQLMutationValidatorInterface::class);
$validator->method('validate')->willReturn([]);
$resolver = new UserCreateResolver($validator);
Field Mismatch
getRules() keys against your GraphQL schema.Overriding Parent Rules
array_merge parent rules.array_merge(parent::getRules(), [...]) to preserve base rules.Performance with Complex Rules
unique checks) in high-traffic mutations.Rule::first() for partial checks.Laravel-Specific Quirks
laravel-bundle.Rule objects or switch to the bundle.Enable Debug Mode
Set debug: true in the bundle config to log validation errors:
# config/packages/assoconnect_graphql_mutation_validator.yaml
debug: true
Inspect Raw Errors
Dump $validator->validate($data) output to debug rule failures:
$errors = $validator->validate($input);
dd($errors); // Array of error messages
Custom Validation Logic
Override validate() to add pre/post-validation logic:
public function validate(array $data): array
{
$this->preValidate($data);
$errors = parent::validate($data);
$this->postValidate($data, $errors);
return $errors;
}
Add Rule Providers Extend the bundle’s rule providers for custom rules:
use Assoconnect\GraphQLMutationValidatorBundle\Validator\RuleProviderInterface;
class CustomRuleProvider implements RuleProviderInterface
{
public function register(): void
{
Validator::extend('custom_rule', function ($attribute, $value, $parameters) {
return $value === $parameters[0];
});
}
}
Localization
Override getMessages() for multi-language support:
protected function getMessages(): array
{
return [
'email.required' => trans('validation.required', ['attribute' => 'Email']),
];
}
Symfony/Laravel Hybrid
Use the laravel-bundle to share Laravel’s validation pipeline:
use Assoconnect\GraphQLMutationValidatorBundle\Validator\LaravelValidator;
class UserValidator extends LaravelValidator
{
protected function rules(): array
{
return [
'email' => 'required|email',
];
}
}
How can I help you explore Laravel packages today?