Skip to content

Commit b2f6dcc

Browse files
committed
feat: update validation of all fields including relations
1 parent c8b5433 commit b2f6dcc

21 files changed

+183
-35
lines changed

src/Contracts/FillableToMany.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
1515
use Illuminate\Database\Eloquent\Model;
1616
use LaravelJsonApi\Eloquent\Polymorphism\MorphMany;
17+
use LaravelJsonApi\Validation\Fields\IsValidated;
1718

18-
interface FillableToMany extends IsReadOnly
19+
interface FillableToMany extends IsReadOnly, IsValidated
1920
{
20-
2121
/**
2222
* Fill the model with the value of the JSON:API to-many relation.
2323
*

src/Contracts/FillableToOne.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
namespace LaravelJsonApi\Eloquent\Contracts;
1313

1414
use Illuminate\Database\Eloquent\Model;
15+
use LaravelJsonApi\Validation\Fields\IsValidated;
1516

16-
interface FillableToOne extends IsReadOnly
17+
interface FillableToOne extends IsReadOnly, IsValidated
1718
{
18-
1919
/**
2020
* Does the model need to exist in the database before the relation is filled?
2121
*

src/Fields/ArrayHash.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,12 @@
1515
use LaravelJsonApi\Core\Json\Hash;
1616
use LaravelJsonApi\Core\Support\Arr;
1717
use LaravelJsonApi\Validation\Fields\IsValidated;
18-
19-
use LaravelJsonApi\Validation\Fields\ValidatedWithKeyedSetOfRules;
20-
18+
use LaravelJsonApi\Validation\Fields\ValidatedWithArrayKeys;
2119
use LaravelJsonApi\Validation\Rules\JsonObject;
2220

23-
use function is_null;
24-
2521
class ArrayHash extends Attribute implements IsValidated
2622
{
27-
use ValidatedWithKeyedSetOfRules;
23+
use ValidatedWithArrayKeys;
2824

2925
/**
3026
* @var Closure|null
@@ -235,7 +231,7 @@ protected function deserialize($value)
235231
$value = ($this->keys)($value);
236232
}
237233

238-
if (is_null($value)) {
234+
if ($value === null) {
239235
return null;
240236
}
241237

@@ -251,7 +247,7 @@ protected function deserialize($value)
251247
*/
252248
protected function assertValue($value): void
253249
{
254-
if ((!is_null($value) && !is_array($value)) || (!empty($value) && !Arr::isAssoc($value))) {
250+
if (($value !== null && !is_array($value)) || (!empty($value) && !Arr::isAssoc($value))) {
255251
throw new \UnexpectedValueException(sprintf(
256252
'Expecting the value of attribute %s to be an associative array.',
257253
$this->name()

src/Fields/ArrayList.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,13 @@
1313

1414
use Illuminate\Support\Arr;
1515
use LaravelJsonApi\Validation\Fields\IsValidated;
16-
use LaravelJsonApi\Validation\Fields\ValidatedWithKeyedSetOfRules;
16+
use LaravelJsonApi\Validation\Fields\ValidatedWithArrayKeys;
1717
use LaravelJsonApi\Validation\Rules\JsonArray;
18-
19-
use function is_null;
2018
use function sort;
2119

2220
class ArrayList extends Attribute implements IsValidated
2321
{
24-
use ValidatedWithKeyedSetOfRules;
22+
use ValidatedWithArrayKeys;
2523

2624
/**
2725
* @var bool
@@ -85,7 +83,7 @@ protected function deserialize($value)
8583
*/
8684
protected function assertValue($value): void
8785
{
88-
if ((!is_null($value) && !is_array($value)) || (!empty($value) && Arr::isAssoc($value))) {
86+
if (($value !== null && !is_array($value)) || (!empty($value) && Arr::isAssoc($value))) {
8987
throw new \UnexpectedValueException(sprintf(
9088
'Expecting the value of attribute %s to be an array list.',
9189
$this->name()

src/Fields/Boolean.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
namespace LaravelJsonApi\Eloquent\Fields;
1313

1414
use LaravelJsonApi\Validation\Fields\IsValidated;
15-
use LaravelJsonApi\Validation\Fields\ValidatedWithListOfRules;
15+
use LaravelJsonApi\Validation\Fields\ValidatedWithRules;
1616
use LaravelJsonApi\Validation\Rules\JsonBoolean;
1717

1818
class Boolean extends Attribute implements IsValidated
1919
{
20-
use ValidatedWithListOfRules;
20+
use ValidatedWithRules;
2121

2222
/**
2323
* Create a boolean attribute.

src/Fields/DateTime.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,15 @@
1212
namespace LaravelJsonApi\Eloquent\Fields;
1313

1414
use Carbon\CarbonInterface;
15-
use Closure;
1615
use Illuminate\Support\Facades\Date;
1716
use LaravelJsonApi\Validation\Fields\IsValidated;
18-
use LaravelJsonApi\Validation\Fields\ValidatedWithListOfRules;
17+
use LaravelJsonApi\Validation\Fields\ValidatedWithRules;
1918
use LaravelJsonApi\Validation\Rules\DateTimeIso8601;
2019
use function config;
2120

2221
class DateTime extends Attribute implements IsValidated
2322
{
24-
use ValidatedWithListOfRules;
23+
use ValidatedWithRules;
2524

2625
/**
2726
* Should dates be converted to the defined time zone?

src/Fields/Integer.php

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

1414
use LaravelJsonApi\Validation\Fields\IsValidated;
15-
use LaravelJsonApi\Validation\Fields\ValidatedWithListOfRules;
15+
use LaravelJsonApi\Validation\Fields\ValidatedWithRules;
1616
use LaravelJsonApi\Validation\Rules\JsonNumber;
1717
use UnexpectedValueException;
1818

1919
class Integer extends Attribute implements IsValidated
2020
{
21-
use ValidatedWithListOfRules;
21+
use ValidatedWithRules;
2222

2323
/**
2424
* @var bool

src/Fields/Number.php

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

1414
use LaravelJsonApi\Validation\Fields\IsValidated;
15-
use LaravelJsonApi\Validation\Fields\ValidatedWithListOfRules;
15+
use LaravelJsonApi\Validation\Fields\ValidatedWithRules;
1616
use LaravelJsonApi\Validation\Rules\JsonNumber;
1717
use UnexpectedValueException;
1818

1919
class Number extends Attribute implements IsValidated
2020
{
21-
use ValidatedWithListOfRules;
21+
use ValidatedWithRules;
2222

2323
/**
2424
* @var bool

src/Fields/Relations/BelongsTo.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@
1414
use Illuminate\Database\Eloquent\Model;
1515
use LaravelJsonApi\Eloquent\Contracts\FillableToOne;
1616
use LaravelJsonApi\Eloquent\Fields\Concerns\IsReadOnly;
17+
use LaravelJsonApi\Validation\Fields\ValidatedWithArrayKeys;
18+
use LaravelJsonApi\Validation\Rules\HasOne as HasOneRule;
1719

1820
class BelongsTo extends ToOne implements FillableToOne
1921
{
2022
use IsReadOnly;
23+
use ValidatedWithArrayKeys;
2124

2225
/**
2326
* Create a belongs-to relation.
@@ -93,4 +96,12 @@ public function associate(Model $model, ?array $identifier): ?Model
9396

9497
return $model->getRelation($this->relationName());
9598
}
99+
100+
/**
101+
* @return array
102+
*/
103+
protected function defaultRules(): array
104+
{
105+
return ['.' => ['array:type,id', new HasOneRule($this)]];
106+
}
96107
}

src/Fields/Relations/BelongsToMany.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,22 @@
1111

1212
namespace LaravelJsonApi\Eloquent\Fields\Relations;
1313

14+
use Closure;
1415
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
1516
use Illuminate\Database\Eloquent\Model;
1617
use Illuminate\Database\Eloquent\Relations\BelongsToMany as EloquentBelongsToMany;
1718
use InvalidArgumentException;
1819
use LaravelJsonApi\Eloquent\Contracts\FillableToMany;
1920
use LaravelJsonApi\Eloquent\Fields\Concerns\IsReadOnly;
21+
use LaravelJsonApi\Validation\Fields\ValidatedWithArrayKeys;
22+
use LaravelJsonApi\Validation\Rules\HasMany as HasManyRule;
23+
use LaravelJsonApi\Validation\Rules\JsonArray;
2024
use function sprintf;
2125

2226
class BelongsToMany extends ToMany implements FillableToMany
2327
{
24-
2528
use IsReadOnly;
29+
use ValidatedWithArrayKeys;
2630

2731
/**
2832
* Create a belongs-to-many relation.
@@ -142,6 +146,17 @@ public function detach(Model $model, array $identifiers): iterable
142146
return $related;
143147
}
144148

149+
/**
150+
* @return array
151+
*/
152+
protected function defaultRules(): array
153+
{
154+
return [
155+
'.' => [new JsonArray(), new HasManyRule($this)],
156+
'*' => ['array:type,id'],
157+
];
158+
}
159+
145160
/**
146161
* @param Model $model
147162
* @return EloquentBelongsToMany

0 commit comments

Comments
 (0)