Send additional action data when returning validationError #122
Replies: 1 comment 1 reply
-
Hi! I'm definitely open to supporting additional parameters but not totally sure what the api would look like.
As a workaround, this should technically work just fine if you ignore the typescript errors.
Nope, not missing anything. export function validationError(
error: ValidatorError,
repopulateFields?: unknown,
init?: ResponseInit
): Response {
return json<ValidationErrorResponseData>(
{
fieldErrors: error.fieldErrors,
subaction: error.subaction,
repopulateFields,
formId: error.formId,
},
{ status: 422, ...init }
);
} Edit: Worth noting though, that this also serves as encapsulation to some degree. If I renamed the |
Beta Was this translation helpful? Give feedback.
-
I am thinking about some imporvements for the design of the
validationError
function, which returns a 422 Response with data of typeValidatorError
:As it is actually a wrapper for a "Bad Request" Response, it would be useful to allow sending additional actionData when the data is not valid. For example, I want to send an additional general error message with when I return a
validationError
, but doing so in Typescript:makes Typescript mad. The workaround would be to use the repopulateFields, but the name is the misleading and it forces to use an unobivous path like
actionData.repopulateField.formError
.I think it would be better to add the possibility to include top-level actionData in addition to the result.error. What do you think?
Many thanks for this awesome library 👏
EDIT: As I'm playing with multiple solution, I noticed that
validationError(result.error)
is the same as writingjson({ ...result.error, formError: "bla bla"}, { status: 422})
. Am I missing something?Beta Was this translation helpful? Give feedback.
All reactions