Skip to content

Commit 6013be5

Browse files
committed
♻️ better attribute validation errors
1 parent dab2a25 commit 6013be5

File tree

5 files changed

+40
-7
lines changed

5 files changed

+40
-7
lines changed

src/Http/Requests/MutateRequest.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Support\Arr;
66
use Illuminate\Validation\Rule;
77
use Lomkit\Rest\Http\Resource;
8+
use Lomkit\Rest\Rules\ArrayWith;
89
use Lomkit\Rest\Rules\CustomRulable;
910

1011
class MutateRequest extends RestRequest
@@ -45,7 +46,7 @@ public function mutateRules(Resource $resource, $prefix = 'mutate.*', $loadedRel
4546
$prefix.'.attributes' => [
4647
'prohibited_if:'.$prefix.'.operation,attach',
4748
'prohibited_if:'.$prefix.'.operation,detach',
48-
'array:'.Arr::join($resource->fields($this), ','),
49+
new ArrayWith($resource->fields($this)),
4950
],
5051
$prefix.'.key' => [
5152
'required_if:'.$prefix.'.operation,update',
@@ -83,9 +84,11 @@ protected function relationRules(Resource $resource, string $prefix = '', $loade
8384

8485
$rules = [
8586
$prefix => [
86-
'array:'.Arr::join($resourceRelationsNotLoaded->map(function ($resourceRelationNotLoaded) {
87-
return $resourceRelationNotLoaded->relation;
88-
})->toArray(), ','),
87+
new ArrayWith(
88+
$resourceRelationsNotLoaded->map(function ($resourceRelationNotLoaded) {
89+
return $resourceRelationNotLoaded->relation;
90+
})->toArray()
91+
),
8992
],
9093
];
9194

src/Relations/BelongsToMany.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Lomkit\Rest\Contracts\RelationResource;
1010
use Lomkit\Rest\Http\Resource;
1111
use Lomkit\Rest\Relations\Traits\HasMultipleResults;
12+
use Lomkit\Rest\Rules\ArrayWith;
1213

1314
class BelongsToMany extends Relation implements RelationResource
1415
{
@@ -30,7 +31,7 @@ public function rules(Resource $resource, string $prefix)
3031
[
3132
$prefix.'.*.pivot' => [
3233
'prohibited_if:'.$prefix.'.*.operation,detach',
33-
'array:'.Arr::join($this->getPivotFields(), ','),
34+
new ArrayWith($this->getPivotFields()),
3435
],
3536
]
3637
);

src/Relations/MorphToMany.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Lomkit\Rest\Contracts\RelationResource;
1010
use Lomkit\Rest\Http\Resource;
1111
use Lomkit\Rest\Relations\Traits\HasMultipleResults;
12+
use Lomkit\Rest\Rules\ArrayWith;
1213

1314
class MorphToMany extends MorphRelation implements RelationResource
1415
{
@@ -30,7 +31,7 @@ public function rules(Resource $resource, string $prefix)
3031
[
3132
$prefix.'.*.pivot' => [
3233
'prohibited_if:'.$prefix.'.*.operation,detach',
33-
'array:'.Arr::join($this->getPivotFields(), ','),
34+
new ArrayWith($this->getPivotFields()),
3435
],
3536
]
3637
);

src/Relations/MorphedByMany.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Lomkit\Rest\Contracts\RelationResource;
1010
use Lomkit\Rest\Http\Resource;
1111
use Lomkit\Rest\Relations\Traits\HasMultipleResults;
12+
use Lomkit\Rest\Rules\ArrayWith;
1213

1314
class MorphedByMany extends MorphRelation implements RelationResource
1415
{
@@ -30,7 +31,7 @@ public function rules(Resource $resource, string $prefix)
3031
[
3132
$prefix.'.*.pivot' => [
3233
'prohibited_if:'.$prefix.'.*.operation,detach',
33-
'array:'.Arr::join($this->getPivotFields(), ','),
34+
new ArrayWith($this->getPivotFields()),
3435
],
3536
]
3637
);

src/Rules/ArrayWith.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Lomkit\Rest\Rules;
4+
5+
use Illuminate\Contracts\Validation\ValidationRule;
6+
7+
class ArrayWith implements ValidationRule
8+
{
9+
protected array $keys;
10+
11+
public function __construct(array $keys)
12+
{
13+
$this->keys = $keys;
14+
}
15+
16+
public function validate(string $attribute, mixed $value, \Closure $fail): void
17+
{
18+
if (!is_array($value)) {
19+
$fail('The '.$attribute.' field must be an array.');
20+
}
21+
foreach (array_keys($value) as $key) {
22+
if (!in_array($key, $this->keys)) {
23+
$fail('The '.$key.' key is not valid for the '.$attribute.' field.');
24+
}
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)