File tree Expand file tree Collapse file tree 4 files changed +79
-2
lines changed
tests/lib/Integration/Fields Expand file tree Collapse file tree 4 files changed +79
-2
lines changed Original file line number Diff line number Diff line change 14
14
use Closure ;
15
15
use LaravelJsonApi \Core \Json \Hash ;
16
16
use LaravelJsonApi \Core \Support \Arr ;
17
+ use LaravelJsonApi \Validation \Fields \IsValidated ;
18
+
19
+ use LaravelJsonApi \Validation \Fields \ValidatedWithKeyedSetOfRules ;
20
+
21
+ use LaravelJsonApi \Validation \Rules \JsonObject ;
22
+
17
23
use function is_null ;
18
24
19
- class ArrayHash extends Attribute
25
+ class ArrayHash extends Attribute implements IsValidated
20
26
{
27
+ use ValidatedWithKeyedSetOfRules;
21
28
22
29
/**
23
30
* @var Closure|null
@@ -48,6 +55,13 @@ class ArrayHash extends Attribute
48
55
*/
49
56
private ?string $ keyCase = null ;
50
57
58
+ /**
59
+ * Whether an empty array is allowed as the value.
60
+ *
61
+ * @var bool
62
+ */
63
+ private bool $ allowEmpty = false ;
64
+
51
65
/**
52
66
* Create an array attribute.
53
67
*
@@ -184,6 +198,19 @@ public function dasherizeKeys(): self
184
198
return $ this ;
185
199
}
186
200
201
+ /**
202
+ * Whether an empty array is allowed as the value.
203
+ *
204
+ * @param bool $allowEmpty
205
+ * @return self
206
+ */
207
+ public function allowEmpty (bool $ allowEmpty = true ): self
208
+ {
209
+ $ this ->allowEmpty = $ allowEmpty ;
210
+
211
+ return $ this ;
212
+ }
213
+
187
214
/**
188
215
* @inheritDoc
189
216
*/
@@ -232,4 +259,11 @@ protected function assertValue($value): void
232
259
}
233
260
}
234
261
262
+ /**
263
+ * @return array<string, mixed>
264
+ */
265
+ protected function defaultRules (): array
266
+ {
267
+ return ['. ' => (new JsonObject ())->allowEmpty ($ this ->allowEmpty )];
268
+ }
235
269
}
Original file line number Diff line number Diff line change 12
12
namespace LaravelJsonApi \Eloquent \Fields ;
13
13
14
14
use Illuminate \Support \Arr ;
15
+ use LaravelJsonApi \Validation \Fields \IsValidated ;
16
+ use LaravelJsonApi \Validation \Fields \ValidatedWithKeyedSetOfRules ;
17
+ use LaravelJsonApi \Validation \Rules \JsonArray ;
18
+
15
19
use function is_null ;
16
20
use function sort ;
17
21
18
- class ArrayList extends Attribute
22
+ class ArrayList extends Attribute implements IsValidated
19
23
{
24
+ use ValidatedWithKeyedSetOfRules;
20
25
21
26
/**
22
27
* @var bool
@@ -88,4 +93,11 @@ protected function assertValue($value): void
88
93
}
89
94
}
90
95
96
+ /**
97
+ * @return array<string, mixed>
98
+ */
99
+ protected function defaultRules (): array
100
+ {
101
+ return ['. ' => new JsonArray ()];
102
+ }
91
103
}
Original file line number Diff line number Diff line change 16
16
use Illuminate \Http \Request ;
17
17
use LaravelJsonApi \Eloquent \Fields \ArrayHash ;
18
18
use LaravelJsonApi \Eloquent \Tests \Integration \TestCase ;
19
+ use LaravelJsonApi \Validation \Fields \IsValidated ;
20
+ use LaravelJsonApi \Validation \Rules \JsonObject ;
19
21
20
22
class ArrayHashTest extends TestCase
21
23
{
@@ -572,4 +574,23 @@ public function testHiddenCallback(): void
572
574
$ this ->assertTrue ($ attr ->isHidden ($ mock ));
573
575
}
574
576
577
+ public function testIsValidated (): void
578
+ {
579
+ $ attr = ArrayHash::make ('permissions ' );
580
+
581
+ $ this ->assertInstanceOf (IsValidated::class, $ attr );
582
+ $ this ->assertEquals ($ expected = ['. ' => new JsonObject ()], $ attr ->rulesForCreation (null ));
583
+ $ this ->assertEquals ($ expected , $ attr ->rulesForUpdate (null , new \stdClass ()));
584
+ }
585
+
586
+ public function testIsValidatedAndAllowsEmpty (): void
587
+ {
588
+ $ attr = ArrayHash::make ('permissions ' )->allowEmpty ();
589
+
590
+ $ this ->assertEquals (
591
+ $ expected = ['. ' => (new JsonObject ())->allowEmpty ()],
592
+ $ attr ->rulesForCreation (null )
593
+ );
594
+ $ this ->assertEquals ($ expected , $ attr ->rulesForUpdate (null , new \stdClass ()));
595
+ }
575
596
}
Original file line number Diff line number Diff line change 16
16
use Illuminate \Http \Request ;
17
17
use LaravelJsonApi \Eloquent \Fields \ArrayList ;
18
18
use LaravelJsonApi \Eloquent \Tests \Integration \TestCase ;
19
+ use LaravelJsonApi \Validation \Fields \IsValidated ;
20
+ use LaravelJsonApi \Validation \Rules \JsonArray ;
19
21
20
22
class ArrayListTest extends TestCase
21
23
{
@@ -327,4 +329,12 @@ public function testHiddenCallback(): void
327
329
$ this ->assertTrue ($ attr ->isHidden ($ mock ));
328
330
}
329
331
332
+ public function testItIsValidated (): void
333
+ {
334
+ $ attr = ArrayList::make ('permissions ' );
335
+
336
+ $ this ->assertInstanceOf (IsValidated::class, $ attr );
337
+ $ this ->assertEquals ($ expected = ['. ' => new JsonArray ()], $ attr ->rulesForCreation (null ));
338
+ $ this ->assertEquals ($ expected , $ attr ->rulesForUpdate (null , new \stdClass ()));
339
+ }
330
340
}
You can’t perform that action at this time.
0 commit comments