Skip to content

Conversation

@sandros94
Copy link
Member

@sandros94 sandros94 commented Jul 5, 2025

resolves #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.

@sandros94 sandros94 self-assigned this Jul 5, 2025
@sandros94 sandros94 requested a review from pi0 as a code owner July 5, 2025 12:24
@sandros94 sandros94 added the enhancement New feature or request label Jul 5, 2025
@sandros94
Copy link
Member Author

Oh, is it normal that we have a decode option for route params but not for queries? 🤔

@codecov
Copy link

codecov bot commented Jul 5, 2025

Codecov Report

❌ 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 ⚠️

📢 Thoughts on this report? Let us know!

@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Jul 6, 2025

Deploying h3dev with  Cloudflare Pages  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

View logs

@sandros94
Copy link
Member Author

@pi0 any update on this?

@pi0
Copy link
Member

pi0 commented Oct 29, 2025

Hi dear @sandros94. It is in my todo list, in meantime do you have time to help on fixing merge conflicts and adressing #1146 (comment) 🙏🏼

@sandros94
Copy link
Member Author

Hi dear @sandros94. It is in my todo list, in meantime do you have time to help on fixing merge conflicts and adressing #1146 (comment) 🙏🏼

Yes absolutely! And sorry, I completely forgot about that review 😅

@sandros94
Copy link
Member Author

I'm terribly sorry, I didn't realize that in 1e4d720 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

@pi0
Copy link
Member

pi0 commented Nov 1, 2025

@sandros94 updated implementation to be more compact API. Love your opinion on latest changes.

@sandros94
Copy link
Member Author

@sandros94 updated implementation to be more compact API. Love your opinion on latest changes.

Thank you, and yes it required a bit of cleanup

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Custom error status for readValidatedBody, readValidatedQuery, etc

3 participants