You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
use Spatie\LaravelData\Data;
final class FooRequest extends Data
{
public function __construct(
public readonly Foo $foo,
) {}
}
and Foo class defined like this:
use Spatie\LaravelData\Support\Creation\CreationContext;
use Spatie\LaravelData\Casts\Cast;
use Spatie\LaravelData\Support\DataProperty;
use Spatie\LaravelData\Concerns\BaseData;
use Spatie\LaravelData\Contracts\BaseData as BaseDataContract;
final readonly class Foo implements BaseDataContract
{
use BaseData;
public function __construct(
public string $value,
) {
}
public static function fromString(string $string): self
{
return new self($string);
}
/** @param array<array-key,mixed> $arguments */
public static function dataCastUsing(array $arguments): Cast
{
return new class () implements Cast {
/** @param CreationContext<Foo> $context */
public function cast(DataProperty $property, mixed $value, array $properties, CreationContext $context): Foo
{
return Foo::fromString((string) $value);
}
};
}
}
In my request I'm sending data like this:
['foo' => 'bar']
And I'm receiving validation errors:
"foo": [
"The foo must be an array."
],
"foo.value": [
"The foo.value field is required."
]
because, I gather, my Foo class contains a value.
I'd like my Foo validation to infer a StringType rather than ArrayType.
I can add these validation rules:
public static function rules(): array
{
return [
'foo' => ['required', 'string'],
'foo.value' => ['sometimes']
];
}
to achieve what I want, but truth be told, this is both separated from my Foo class (I'd like every Request class where I'm using my Foo to behave the same way without duplicating rules) and bizarre (as I have to define rule for foo.value which, at this point, shouldn't even exist as foo is a string).
What can I do to my Foo class, apart for changing the constructor, so that the validation will treat it like a string.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I've got a Request class defined like this:
and Foo class defined like this:
In my request I'm sending data like this:
And I'm receiving validation errors:
because, I gather, my Foo class contains a
value
.I'd like my Foo validation to infer a StringType rather than ArrayType.
I can add these validation rules:
to achieve what I want, but truth be told, this is both separated from my Foo class (I'd like every Request class where I'm using my Foo to behave the same way without duplicating
rules
) and bizarre (as I have to define rule forfoo.value
which, at this point, shouldn't even exist asfoo
is a string).What can I do to my Foo class, apart for changing the constructor, so that the validation will treat it like a string.
Beta Was this translation helpful? Give feedback.
All reactions