Following the lighthouse validation in docs you first you add @validator
to the schema.
type Mutation {
createUser(
name: String
age: Int!
): User @create @validator
}
Then you create that validator with php artisan lighthouse:validator CreateUserValidator
.
On the file, you do the before laravel validation rule.
<?php
namespace App\GraphQL\Validators;
use Nuwave\Lighthouse\Validation\Validator;
class CreateUserValidator extends Validator
{
/**
* Return the validation rules.
*
* @return array<string, array<mixed>>
*/
public function rules(): array
{
return [
'name' => [
'required',
'min:2'
],
'age' => [
'required',
'date',
'before:-10 years'
],
];
}
}