Skip to content

Commit cd4a0d0

Browse files
committed
Run validators for null values
1 parent 6c2bf3d commit cd4a0d0

File tree

6 files changed

+13
-8
lines changed

6 files changed

+13
-8
lines changed

src/Schema/Concerns/SetsValue.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,8 @@ public function deserializeValue(mixed $value, Context $context): mixed
129129
*/
130130
public function validateValue(mixed $value, callable $fail, Context $context): void
131131
{
132-
if ($value === null) {
133-
if (!$this->nullable) {
134-
$fail('must not be null');
135-
}
136-
return;
132+
if ($value === null && !$this->nullable) {
133+
$fail('must not be null');
137134
}
138135

139136
foreach ($this->validators as $validator) {

src/Schema/Field/Boolean.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public function __construct(string $name)
1111
$this->serialize(static fn($value) => (bool) $value);
1212

1313
$this->validate(static function (mixed $value, callable $fail): void {
14-
if (!is_bool($value)) {
14+
if (!is_bool($value) && $value !== null) {
1515
$fail('must be a boolean');
1616
}
1717
});

src/Schema/Field/Date.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function __construct(string $name)
2929
});
3030

3131
$this->validate(static function (mixed $value, callable $fail): void {
32-
if (!$value instanceof \DateTime) {
32+
if (!$value instanceof \DateTime && $value !== null) {
3333
$fail('must be a date');
3434
}
3535
});

src/Schema/Field/DateTime.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function __construct(string $name)
2727
});
2828

2929
$this->validate(static function (mixed $value, callable $fail): void {
30-
if (!$value instanceof \DateTime) {
30+
if (!$value instanceof \DateTime && $value !== null) {
3131
$fail('must be a date-time');
3232
}
3333
});

src/Schema/Field/Number.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ public function __construct(string $name)
1919
$this->serialize(static fn($value) => (float) $value);
2020

2121
$this->validate(function (mixed $value, callable $fail): void {
22+
if ($value === null) {
23+
return;
24+
}
25+
2226
if (!is_numeric($value)) {
2327
$fail('must be numeric');
2428
return;

src/Schema/Field/Str.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ public function __construct(string $name)
1717
$this->serialize(static fn($value) => (string) $value);
1818

1919
$this->validate(function (mixed $value, callable $fail): void {
20+
if ($value === null) {
21+
return;
22+
}
23+
2024
if (!is_string($value)) {
2125
$fail('must be a string');
2226
return;

0 commit comments

Comments
 (0)