Optional nested data failed validation (v2) #128
Replies: 4 comments 5 replies
-
Hi @davidkvasnovsky, So in this case the coordinates property should be nullable due to the |
Beta Was this translation helpful? Give feedback.
-
In this state
call
will occur. Can improve this problem ? |
Beta Was this translation helpful? Give feedback.
-
@rubenvanassche I get similar issue and seem not fixed in v2, the only solution seem to use #[WithoutValidation].. My UserData name and email: public string|Lazy|null $name = null;
public string|Lazy|null $email = null; With validation rules() method in UserData. However when I try to update a comment by passing CommentData: //#[WithoutValidation]
public UserData|Optional|Lazy|null $user = null, Am I missing something? Thanks |
Beta Was this translation helpful? Give feedback.
-
Seems, I have similar issue or do something wrong Here is my data structure: CreateUserRequest.php class CreateUserRequest extends Data
{
public function __construct(
public string $first_name,
public string $last_name,
public string $email,
public ?Address $address, //public Optional|Address $address - also tried such syntax
) {
}
public static function rules(): array
{
return [
'first_name' => 'required|string',
'last_name' => 'required|string',
'email' => 'required|email',
'address' => 'nullable|array:street,postcode,city,country',
];
}
} Address.php class Address extends Data
{
public function __construct(
public string $street,
public string $postcode,
public string $city,
public string $country,
) {
}
public static function rules(array $payload): array
{
return [
'street' => 'required|string',
'postcode' => 'required|string',
'city' => 'required|string',
'country' => 'required|string',
];
}
} UserController.php class UserController extends Controller
{
public function store(CreateUserRequest $createUserRequest)
{
// some logic
}
} When I send a request with following payload: {
"first_name": "John",
"last_name": "Doe",
"email": "[email protected]",
} I get validation errors: {
"message": "The address.street field is required. (and 3 more errors)",
"errors": {
"address.street": [
"The address.street field is required."
],
"address.postcode": [
"The address.postcode field is required."
],
"address.city": [
"The address.city field is required."
],
"address.country": [
"The address.country field is required."
]
}
} I am expecting, that address object won't be validated with such JSON payload and can be nullable. How can I handle such case? Thank you in advance |
Beta Was this translation helpful? Give feedback.
-
Let's assume we have following data structure:
UserData.php
AddressData.php
CoordsData.php
CreateUserController.php
I deliberately specified
CreateUserController.php
because this bug only occurs if the data is coming from a request object.So let's make a
POST
request with JSON payload, that is given bellow:We get the following error:
I assume that this behavior is not correct. It is possible to solve it temporarily by adding a validation rule
#[WithoutValidation]
, but that doesn't seem very optimal.Beta Was this translation helpful? Give feedback.
All reactions