Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@sandros94
Copy link
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)),
    }),
    (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
  | ((issues: ValidateIssues) => 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 95.08197% with 3 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/utils/internal/validate.ts 93.87% 3 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

age: v.pipe(v.number(), v.integer(), v.minValue(1)),
}),
{
onError: (issues) => ({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can input be in { issues } format (or we pass whole result to format error?

Both for forward compatibility and also support custom errors

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