-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Description
Let's say this is not a bug report.
Description
A common problem with the serializer and the use of the deserialization is that it will throw errors if a wrong value is inside the given data. This stops any possibility of validation because the deserialization will not end. For information, it is a known issue of ApiPlatform. See api-platform/core#1157, api-platform/core#1735 and api-platform/core#1352.
I suggest adding a new context option that will contain an "error bag". Then we could return the object deserialized and ready for validation with the validation component, as well as a list of errors that occured in the deserialization process.
This way you can return all the validation error properly.
Example
$errors = new ErrorBag();
$myObject = $serializer->deserialize($data, MyObject::class, 'json', ['error_bag' => $errors]);
$contraints = $validator->validate($myObject); // And we're happy here
// Then I can foreach on $errors and $contraints to generate a correct & complete output.
POC
I tried to make this feature (it's a far more simple version than what it should actually be). It's here: https://github.com/Nek-/poc-symfony-deserialization-errors
I can implement this feature if feedbacks are positive.