Hi,
I am trying to group all backend validations into a single error object that is returned to the frontend so the user can know at once all they have to fix.
To do this I am trying to use RedwoodRecord
since it already returns a nicely formatted output.
For example, with my current contact model from the tutorial I am getting this error output:
{
base: [],
name: [],
email: [ 'email must be formatted like an email address' ],
message: []
}
and then what I am doing with that output is:
throw new UserInputError("Can't create new Contact", {
messages: contact.errors,
})
First issue I encountered with this structure is that when passing the error object to the Form
component it marks all fields as with errors because it receives an empty array instead of undefine I assume. This is easily fixable.
The second issue that I am getting and it’s mainly why I am creating this topic is the following:
The FormError
component when getting a structure like that lists the field errors in a list and prefixes the errors with the name of the field, the key in the object. Why is this a problem? In the Blog tutorial the error is created manually and does not include the name of the field already. But both the validate
default messages and the RedwoodRecord
validation errors include the name of the field already so I am getting the following error message displayed:
Can't create new Contact
email email must be formatted like an email address
So my question is, why does the FormError
add as a prefix the name of the field? Shouldn’t that be handled in the api? the api should provide user ready errors. Specially if the intention is that in the future a project can have multiple frontends.
And as I already mentioned this is kind of already being handled by the api anyways so that’s why I get the double name of the field at the begining.
When using validate
from the api lib I am not getting this error but this is just because it throws an error on a per-field basis so once a field is invalid it returns a single error not with the structure I showed.
I assume that if I put a try-catch for each validation and agregate them with that structure, the same would happen.
I can work on fixing the issue with the empty arrays being marked as errors but would love some more discussion around the other issue before starting to implement anything since I see backwards compatibility issue with just removing the field name prefix on FormError