Skip to content

Commit 74403d6

Browse files
committed
feat: add validation to array hash and list attributes
1 parent 5989b17 commit 74403d6

File tree

4 files changed

+79
-2
lines changed

4 files changed

+79
-2
lines changed

src/Fields/ArrayHash.php

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,17 @@
1414
use Closure;
1515
use LaravelJsonApi\Core\Json\Hash;
1616
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+
1723
use function is_null;
1824

19-
class ArrayHash extends Attribute
25+
class ArrayHash extends Attribute implements IsValidated
2026
{
27+
use ValidatedWithKeyedSetOfRules;
2128

2229
/**
2330
* @var Closure|null
@@ -48,6 +55,13 @@ class ArrayHash extends Attribute
4855
*/
4956
private ?string $keyCase = null;
5057

58+
/**
59+
* Whether an empty array is allowed as the value.
60+
*
61+
* @var bool
62+
*/
63+
private bool $allowEmpty = false;
64+
5165
/**
5266
* Create an array attribute.
5367
*
@@ -184,6 +198,19 @@ public function dasherizeKeys(): self
184198
return $this;
185199
}
186200

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+
187214
/**
188215
* @inheritDoc
189216
*/
@@ -232,4 +259,11 @@ protected function assertValue($value): void
232259
}
233260
}
234261

262+
/**
263+
* @return array<string, mixed>
264+
*/
265+
protected function defaultRules(): array
266+
{
267+
return ['.' => (new JsonObject())->allowEmpty($this->allowEmpty)];
268+
}
235269
}

src/Fields/ArrayList.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,16 @@
1212
namespace LaravelJsonApi\Eloquent\Fields;
1313

1414
use Illuminate\Support\Arr;
15+
use LaravelJsonApi\Validation\Fields\IsValidated;
16+
use LaravelJsonApi\Validation\Fields\ValidatedWithKeyedSetOfRules;
17+
use LaravelJsonApi\Validation\Rules\JsonArray;
18+
1519
use function is_null;
1620
use function sort;
1721

18-
class ArrayList extends Attribute
22+
class ArrayList extends Attribute implements IsValidated
1923
{
24+
use ValidatedWithKeyedSetOfRules;
2025

2126
/**
2227
* @var bool
@@ -88,4 +93,11 @@ protected function assertValue($value): void
8893
}
8994
}
9095

96+
/**
97+
* @return array<string, mixed>
98+
*/
99+
protected function defaultRules(): array
100+
{
101+
return ['.' => new JsonArray()];
102+
}
91103
}

tests/lib/Integration/Fields/ArrayHashTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
use Illuminate\Http\Request;
1717
use LaravelJsonApi\Eloquent\Fields\ArrayHash;
1818
use LaravelJsonApi\Eloquent\Tests\Integration\TestCase;
19+
use LaravelJsonApi\Validation\Fields\IsValidated;
20+
use LaravelJsonApi\Validation\Rules\JsonObject;
1921

2022
class ArrayHashTest extends TestCase
2123
{
@@ -572,4 +574,23 @@ public function testHiddenCallback(): void
572574
$this->assertTrue($attr->isHidden($mock));
573575
}
574576

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+
}
575596
}

tests/lib/Integration/Fields/ArrayListTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
use Illuminate\Http\Request;
1717
use LaravelJsonApi\Eloquent\Fields\ArrayList;
1818
use LaravelJsonApi\Eloquent\Tests\Integration\TestCase;
19+
use LaravelJsonApi\Validation\Fields\IsValidated;
20+
use LaravelJsonApi\Validation\Rules\JsonArray;
1921

2022
class ArrayListTest extends TestCase
2123
{
@@ -327,4 +329,12 @@ public function testHiddenCallback(): void
327329
$this->assertTrue($attr->isHidden($mock));
328330
}
329331

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+
}
330340
}

0 commit comments

Comments
 (0)