Skip to content

feat: customizable validation errors#1146

Merged
pi0 merged 20 commits into
mainfrom
feat/custom-validation-errors
Dec 30, 2025
Merged

feat: customizable validation errors#1146
pi0 merged 20 commits into
mainfrom
feat/custom-validation-errors

Conversation

@sandros94

@sandros94 sandros94 commented Jul 5, 2025

Copy link
Copy Markdown
Member

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
Copy Markdown
Member Author

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

@codecov

codecov Bot commented Jul 5, 2025

Copy link
Copy Markdown

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!

Comment thread src/handler.ts Outdated
Comment thread src/utils/body.ts Outdated
@cloudflare-workers-and-pages

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

Copy link
Copy Markdown

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

Comment thread docs/2.utils/1.request.md Outdated
@sandros94

Copy link
Copy Markdown
Member Author

@pi0 any update on this?

@pi0

pi0 commented Oct 29, 2025

Copy link
Copy Markdown
Member

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
Copy Markdown
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
Copy Markdown
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

pi0 commented Nov 1, 2025

Copy link
Copy Markdown
Member

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

@sandros94

Copy link
Copy Markdown
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

@coderabbitai

coderabbitai Bot commented Dec 30, 2025

Copy link
Copy Markdown

Warning

Rate limit exceeded

@pi0 has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 11 minutes and 33 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between d6b5f2a and f85c8cf.

📒 Files selected for processing (7)
  • docs/2.utils/1.request.md
  • src/handler.ts
  • src/utils/body.ts
  • src/utils/internal/validate.ts
  • src/utils/request.ts
  • test/handler.test.ts
  • test/validate.test.ts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@pi0
pi0 merged commit 1ec9c94 into main Dec 30, 2025
5 of 6 checks passed
@pi0
pi0 deleted the feat/custom-validation-errors branch December 30, 2025 14:04
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

2 participants