Skip to content

Commit 1d9a659

Browse files
authored
Merge pull request #861 from kenjis/refactor-validation-rules
refactor: validation rules
2 parents 59cc3dc + 8badb41 commit 1d9a659

File tree

8 files changed

+135
-100
lines changed

8 files changed

+135
-100
lines changed

docs/customization/validation_rules.md

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ Shield has the following rules for registration by default:
2727
],
2828
'password' => [
2929
'label' => 'Auth.password',
30-
'rules' => 'required|max_byte[72]|strong_password[]',
30+
'rules' => [
31+
'required',
32+
'max_byte[72]',
33+
'strong_password[]',
34+
],
3135
'errors' => [
3236
'max_byte' => 'Auth.errorPasswordTooLongBytes'
3337
]
@@ -98,8 +102,13 @@ Similar to the process for validation rules in the **Registration** section, you
98102
//--------------------------------------------------------------------
99103
public $login = [
100104
// 'username' => [
101-
// 'label' => 'Auth.username',
102-
// 'rules' => 'required|max_length[30]|min_length[3]|regex_match[/\A[a-zA-Z0-9\.]+\z/]',
105+
// 'label' => 'Auth.username',
106+
// 'rules' => [
107+
// 'required',
108+
// 'max_length[30]',
109+
// 'min_length[3]',
110+
// 'regex_match[/\A[a-zA-Z0-9\.]+\z/]',
111+
// ],
103112
// ],
104113
'email' => [
105114
'label' => 'Auth.email',
@@ -111,7 +120,10 @@ public $login = [
111120
],
112121
'password' => [
113122
'label' => 'Auth.password',
114-
'rules' => 'required|max_byte[72]',
123+
'rules' => [
124+
'required',
125+
'max_byte[72]',
126+
],
115127
'errors' => [
116128
'max_byte' => 'Auth.errorPasswordTooLongBytes',
117129
]

src/Commands/User.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use CodeIgniter\Shield\Entities\User as UserEntity;
1414
use CodeIgniter\Shield\Exceptions\UserNotFoundException;
1515
use CodeIgniter\Shield\Models\UserModel;
16-
use CodeIgniter\Shield\Validation\RegistrationValidationRules;
16+
use CodeIgniter\Shield\Validation\ValidationRules;
1717
use Config\Services;
1818

1919
class User extends BaseCommand
@@ -219,9 +219,9 @@ private function setTables(): void
219219

220220
private function setValidationRules(): void
221221
{
222-
$validationRules = new RegistrationValidationRules();
222+
$validationRules = new ValidationRules();
223223

224-
$rules = $validationRules->get();
224+
$rules = $validationRules->getRegistrationRules();
225225

226226
// Remove `strong_password` because it only supports use cases
227227
// to check the user's own password.

src/Config/Auth.php

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -203,26 +203,36 @@ class Auth extends BaseConfig
203203
* The validation rules for username
204204
* --------------------------------------------------------------------
205205
*
206-
* @var string[]
206+
* Do not use string rules like `required|valid_email`.
207+
*
208+
* @var array<string, array<int, string>|string>
207209
*/
208210
public array $usernameValidationRules = [
209-
'required',
210-
'max_length[30]',
211-
'min_length[3]',
212-
'regex_match[/\A[a-zA-Z0-9\.]+\z/]',
211+
'label' => 'Auth.username',
212+
'rules' => [
213+
'required',
214+
'max_length[30]',
215+
'min_length[3]',
216+
'regex_match[/\A[a-zA-Z0-9\.]+\z/]',
217+
],
213218
];
214219

215220
/**
216221
* --------------------------------------------------------------------
217222
* The validation rules for email
218223
* --------------------------------------------------------------------
219224
*
220-
* @var string[]
225+
* Do not use string rules like `required|valid_email`.
226+
*
227+
* @var array<string, array<int, string>|string>
221228
*/
222229
public array $emailValidationRules = [
223-
'required',
224-
'max_length[254]',
225-
'valid_email',
230+
'label' => 'Auth.email',
231+
'rules' => [
232+
'required',
233+
'max_length[254]',
234+
'valid_email',
235+
],
226236
];
227237

228238
/**

src/Controllers/LoginController.php

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
use App\Controllers\BaseController;
88
use CodeIgniter\HTTP\RedirectResponse;
99
use CodeIgniter\Shield\Authentication\Authenticators\Session;
10-
use CodeIgniter\Shield\Authentication\Passwords;
1110
use CodeIgniter\Shield\Traits\Viewable;
11+
use CodeIgniter\Shield\Validation\ValidationRules;
1212

1313
class LoginController extends BaseController
1414
{
@@ -82,23 +82,9 @@ public function loginAction(): RedirectResponse
8282
*/
8383
protected function getValidationRules(): array
8484
{
85-
return setting('Validation.login') ?? [
86-
// 'username' => [
87-
// 'label' => 'Auth.username',
88-
// 'rules' => config('Auth')->usernameValidationRules,
89-
// ],
90-
'email' => [
91-
'label' => 'Auth.email',
92-
'rules' => config('Auth')->emailValidationRules,
93-
],
94-
'password' => [
95-
'label' => 'Auth.password',
96-
'rules' => 'required|' . Passwords::getMaxLengthRule(),
97-
'errors' => [
98-
'max_byte' => 'Auth.errorPasswordTooLongBytes',
99-
],
100-
],
101-
];
85+
$rules = new ValidationRules();
86+
87+
return $rules->getLoginRules();
10288
}
10389

10490
/**

src/Controllers/MagicLinkController.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,7 @@ private function recordLoginAttempt(
234234
protected function getValidationRules(): array
235235
{
236236
return [
237-
'email' => [
238-
'label' => 'Auth.email',
239-
'rules' => config('Auth')->emailValidationRules,
240-
],
237+
'email' => config('Auth')->emailValidationRules,
241238
];
242239
}
243240
}

src/Controllers/RegisterController.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use CodeIgniter\Shield\Exceptions\ValidationException;
1515
use CodeIgniter\Shield\Models\UserModel;
1616
use CodeIgniter\Shield\Traits\Viewable;
17-
use CodeIgniter\Shield\Validation\RegistrationValidationRules;
17+
use CodeIgniter\Shield\Validation\ValidationRules;
1818
use Psr\Log\LoggerInterface;
1919

2020
/**
@@ -167,8 +167,8 @@ protected function getUserEntity(): User
167167
*/
168168
protected function getValidationRules(): array
169169
{
170-
$rules = new RegistrationValidationRules();
170+
$rules = new ValidationRules();
171171

172-
return $rules->get();
172+
return $rules->getRegistrationRules();
173173
}
174174
}

src/Validation/RegistrationValidationRules.php

Lines changed: 0 additions & 59 deletions
This file was deleted.

src/Validation/ValidationRules.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CodeIgniter\Shield\Validation;
6+
7+
use CodeIgniter\Shield\Authentication\Passwords;
8+
use CodeIgniter\Shield\Config\Auth;
9+
10+
class ValidationRules
11+
{
12+
private Auth $config;
13+
14+
/**
15+
* Auth Table names
16+
*/
17+
private array $tables;
18+
19+
public function __construct()
20+
{
21+
/** @var Auth $authConfig */
22+
$authConfig = config('Auth');
23+
24+
$this->config = $authConfig;
25+
$this->tables = $this->config->tables;
26+
}
27+
28+
public function getRegistrationRules(): array
29+
{
30+
helper('setting');
31+
32+
$setting = setting('Validation.registration');
33+
if ($setting !== null) {
34+
return $setting;
35+
}
36+
37+
$usernameRules = $this->config->usernameValidationRules;
38+
$usernameRules['rules'][] = sprintf(
39+
'is_unique[%s.username]',
40+
$this->tables['users']
41+
);
42+
43+
$emailRules = $this->config->emailValidationRules;
44+
$emailRules['rules'][] = sprintf(
45+
'is_unique[%s.secret]',
46+
$this->tables['identities']
47+
);
48+
49+
$passwordRules = $this->getPasswordRules();
50+
$passwordRules['rules'][] = 'strong_password[]';
51+
52+
return [
53+
'username' => $usernameRules,
54+
'email' => $emailRules,
55+
'password' => $passwordRules,
56+
'password_confirm' => $this->getPasswordConfirmRules(),
57+
];
58+
}
59+
60+
public function getLoginRules(): array
61+
{
62+
helper('setting');
63+
64+
return setting('Validation.login') ?? [
65+
// 'username' => $this->config->usernameValidationRules,
66+
'email' => $this->config->emailValidationRules,
67+
'password' => $this->getPasswordRules(),
68+
];
69+
}
70+
71+
public function getPasswordRules(): array
72+
{
73+
return [
74+
'label' => 'Auth.password',
75+
'rules' => ['required', Passwords::getMaxLengthRule()],
76+
'errors' => [
77+
'max_byte' => 'Auth.errorPasswordTooLongBytes',
78+
],
79+
];
80+
}
81+
82+
public function getPasswordConfirmRules(): array
83+
{
84+
return [
85+
'label' => 'Auth.passwordConfirm',
86+
'rules' => 'required|matches[password]',
87+
];
88+
}
89+
}

0 commit comments

Comments
 (0)