Custom error status for readValidatedBody, readValidatedQuery, etc
Describe the feature
I love readValidatedBody but 400 as status error is too generic, I use it for all sort of errors and I would like to have the ability to set a better error code.
Ideally it would simply be: readValidatedBody(event, schema.parse, 422)
it can be 400 as default so it wouldn't be a breaking change, even tough I would personally prefer 422
Additional information
- [ ] Would you be willing to help implement this feature?
PR welcome. Perhaps we could keep it more generic like readValidatedBody(event, schema, { failStatus: 422 }). /cc @sandros94 what do you think?
PR welcome. Perhaps we could keep it more generic like
readValidatedBody(event, schema, { failStatus: 422 }). /cc @sandros94 what do you think?
Yes, an api that I usually add into my own projects (focused on valibot tho) is something like:
function createValidationError<DataT = unknown>(
error: any,
overrides?: Partial<H3Error<DataT>> & {
status?: number
statusText?: string
},
) {
return createError({
status: 400,
statusMessage: 'Validation failed',
...overrides,
data: error,
message: v.isValiError(error)
? v.summarize(error.issues)
: error.message
? error.message
: undefined,
})
}
Used indeed as readValidatedBody(event, schema, { statusMessage: 'My Message' })
But rethinking about this it would be nice to access the ReadonlyArray<Issue> that comes from Standard-Schema's issues via a callback or similar, WDYT?
I did experiment with this last weekend, I do plan to work on what I think is a nice and flexible implementation in the upcoming days/weeks