File tree Expand file tree Collapse file tree 4 files changed +45
-0
lines changed Expand file tree Collapse file tree 4 files changed +45
-0
lines changed Original file line number Diff line number Diff line change @@ -126,6 +126,7 @@ You can use them in the same way as Laravel's base rules:
126126``` php
127127use Maize\Encryptable\Rules\ExistsEncrypted;
128128use Illuminate\Support\Facades\Validator;
129+ use Illuminate\Validation\Rule;
129130
130131$data = [
131132 'email' => 'email@example.com',
@@ -137,6 +138,7 @@ Validator::make($data, [
137138 'string',
138139 'email',
139140 new ExistsEncrypted('users'), // checks whether the given email exists in the database
141+ Rule::existsEncrypted('users') // alternative way to invoke the rule
140142 ],
141143]);
142144```
@@ -155,6 +157,7 @@ Validator::make($data, [
155157 'string',
156158 'email',
157159 new UniqueEncrypted('users'), // checks whether the given email does not already exist in the database
160+ Rule::uniqueEncrypted('users') // alternative way to invoke the rule
158161 ],
159162]);
160163```
Original file line number Diff line number Diff line change 22
33namespace Maize \Encryptable ;
44
5+ use Illuminate \Validation \Rule ;
6+ use Maize \Encryptable \Rules \ExistsEncrypted ;
7+ use Maize \Encryptable \Rules \UniqueEncrypted ;
58use Spatie \LaravelPackageTools \Package ;
69use Spatie \LaravelPackageTools \PackageServiceProvider ;
710
@@ -13,4 +16,17 @@ public function configurePackage(Package $package): void
1316 ->name ('laravel-encryptable ' )
1417 ->hasConfigFile ();
1518 }
19+
20+ public function bootingPackage ()
21+ {
22+ Rule::macro (
23+ 'uniqueEncrypted ' ,
24+ fn (string $ table , string $ column = 'NULL ' ) => new UniqueEncrypted ($ table , $ column )
25+ );
26+
27+ Rule::macro (
28+ 'existsEncrypted ' ,
29+ fn (string $ table , string $ column = 'NULL ' ) => new ExistsEncrypted ($ table , $ column )
30+ );
31+ }
1632}
Original file line number Diff line number Diff line change 33namespace Maize \Encryptable \Tests ;
44
55use Illuminate \Support \Facades \Validator ;
6+ use Illuminate \Validation \Rule ;
67use Maize \Encryptable \Rules \ExistsEncrypted ;
78
89class ExistsEncryptedTest extends TestCase
@@ -24,4 +25,16 @@ public function it_should_validate_encrypted_data_with_custom_exists_rule()
2425
2526 $ this ->assertFalse ($ validationFails );
2627 }
28+
29+ /** @test */
30+ public function it_should_have_rule_macro ()
31+ {
32+ $ user = $ this ->createUser ();
33+
34+ $ validationFails = Validator::make ($ user ->toArray (), [
35+ 'first_name ' => Rule::existsEncrypted ('users ' ),
36+ ])->fails ();
37+
38+ $ this ->assertFalse ($ validationFails );
39+ }
2740}
Original file line number Diff line number Diff line change 33namespace Maize \Encryptable \Tests ;
44
55use Illuminate \Support \Facades \Validator ;
6+ use Illuminate \Validation \Rule ;
67use Maize \Encryptable \Rules \UniqueEncrypted ;
78
89class UniqueEncryptedTest extends TestCase
@@ -24,4 +25,16 @@ public function it_should_validate_encrypted_data_with_custom_unique_rule()
2425
2526 $ this ->assertTrue ($ validationFails );
2627 }
28+
29+ /** @test */
30+ public function it_should_have_rule_macro ()
31+ {
32+ $ user = $ this ->createUser ();
33+
34+ $ validationFails = Validator::make ($ user ->toArray (), [
35+ 'first_name ' => Rule::uniqueEncrypted ('users ' ),
36+ ])->fails ();
37+
38+ $ this ->assertTrue ($ validationFails );
39+ }
2740}
You can’t perform that action at this time.
0 commit comments