how can i ignore username / email / phone validation check for current user when updating?
update: i also used class validation but user is null and cant reach the current user id there
updateUserMutator(input: UpdateUserInput! ): User @guard(with: ["api"])
input UpdateUserInput {
name: String! @rules(apply: ["min:1","max:255", "required"])
lastname: String! @rules(apply: ["min:1", "max:255", "required"])
username: String! @rules(apply: ["min:1","max:12", "unique:users,username", "regex:/^[a-zA-Z0-9]+/"])
email: String! @rules(apply: ["email", "unique:users,email", "required"])
phone: String! @rules(apply: ["required", "unique:users,phone", "regex:/[0-9]{10}/"])
}
used class validation:
final class UpdateUserInputValidator extends Validator
{
public function authorize()
{
return true;
}
/**
* Return the validation rules.
*
* @return array<string, array<mixed>>
*/
public function rules(): array
{
// get user from context
$id = auth()->id();
return [
'name' => ['sometimes', 'string', 'between:2,25'],
'lastname' => ['sometimes', 'string', 'between:2,25'],
'username' => [
'sometimes',
Rule::unique('users', 'username')->ignore($id, 'id'),
],
'email' => [
'sometimes',
'string',
'email',
'max:255',
Rule::unique('users', 'email')->ignore($id, 'id'),
],
'phone' => [
'sometimes',
'regex:/[0-9]{10}/',
Rule::unique('users', 'phone')->ignore($id, 'id'),
],
];
}
}
but user is null there.
>Solution :
You forget to update the upper part
input UpdateUserInput @validator {
name: String
lastname: String
username: String
email: String
phone: String
}