Skip to content

Commit

Permalink
Merge pull request #4103 from GCTC-NTGC/bug/improve-unique-email-vali…
Browse files Browse the repository at this point in the history
…dation

Bug - Improve unique email validation
  • Loading branch information
esizer authored Sep 28, 2022
2 parents 38f2263 + 5a3f669 commit 4aa9bbc
Show file tree
Hide file tree
Showing 19 changed files with 1,388 additions and 1,032 deletions.
44 changes: 44 additions & 0 deletions api/app/GraphQL/Validators/UpdateUserInputValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace App\GraphQL\Validators;

use Illuminate\Validation\Rule;
use Nuwave\Lighthouse\Validation\Validator;

final class UpdateUserInputValidator extends Validator
{
/**
* Return the validation rules.
*
* @return array<string, array<mixed>>
*/
public function rules(): array
{
return [
'email' => [
'sometimes',
'nullable',
/**
* Note: Ignore the email for the user passed
* in when executing the mutation so it is not
* included in the unique check. This is required
* for allowing a user to be updated while the email
* remains the same.
*
* REF: https://laravel.com/docs/9.x/validation#rule-unique
*/
Rule::unique('users', 'email')->ignore($this->arg('id'), 'id'),
]
];
}

/**
* Return the validation messages
*/
public function messages(): array
{
return [
'email.unique' => 'This email address is already in use',
];
}
}
Loading

0 comments on commit 4aa9bbc

Please sign in to comment.