feat: customizable validation errors
resolves https://github.com/h3js/h3/issues/982
I've added an additional argument to each readValidatedBody, getValidatedQuery and getValidatedRouteParams which can be either an error object or a function that returns one. If a standard-schema is being used, then the issues array is available as an argument for the function variant.
This allows for the following custom error, which will ouput a markdown list of all the errors recorded:
import * as v from "valibot";
app.post("/", async (event) => {
const body = await readValidatedBody(
event,
v.object({
name: v.pipe(v.string(), v.minLength(3), v.maxLength(20)),
age: v.pipe(v.number(), v.integer(), v.minValue(1)),
}),
{
onError: ({ issues }) => ({
statusText: "Custom validation error",
message: v.summarize(issues),
}),
},
);
});
I've also updated the defineValidatedHandler, which brings three more properties: bodyErrors, headersErrors and queryErrors. All allowing for either error object or function that returns one
Added two types, but I'm not super satisfied with ValidateIssues naming (I'm open for suggestions):
export type ValidateIssues = ReadonlyArray<Issue>;
export type ValidateError =
| (() => ErrorDetails)
| ((result: FailureResult) => ErrorDetails);
I've also made sure that if a new HTTPError is thrown during validation, its content will be directly passed as is, quite useful for non-standard-schema validations.
Oh, is it normal that we have a decode option for route params but not for queries? 🤔
Codecov Report
:x: Patch coverage is 93.33333% with 2 lines in your changes missing coverage. Please review.
| Files with missing lines | Patch % | Lines |
|---|---|---|
| src/utils/internal/validate.ts | 91.66% | 2 Missing :warning: |
:loudspeaker: Thoughts on this report? Let us know!
Deploying h3dev with
Cloudflare Pages
| Latest commit: |
9f96225
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://69c3837d.h3dev.pages.dev |
| Branch Preview URL: | https://feat-custom-validation-error.h3dev.pages.dev |
@pi0 any update on this?
Hi dear @sandros94. It is in my todo list, in meantime do you have time to help on fixing merge conflicts and adressing https://github.com/h3js/h3/pull/1146#discussion_r2188235982 🙏🏼
Hi dear @sandros94. It is in my todo list, in meantime do you have time to help on fixing merge conflicts and adressing https://github.com/h3js/h3/pull/1146#discussion_r2188235982 🙏🏼
Yes absolutely! And sorry, I completely forgot about that review 😅
I'm terribly sorry, I didn't realize that in 1e4d72079488c1d63bcb3f82491b8559883bbb71 I did not update the docs 🙈
For now I would leave it as only { issues }, to better follow the standard-schema spec FailureResult and leave room for future expansion.
Let me know if there is anything else I should take care of
@sandros94 updated implementation to be more compact API. Love your opinion on latest changes.
@sandros94 updated implementation to be more compact API. Love your opinion on latest changes.
Thank you, and yes it required a bit of cleanup