Skip to content

Commit 1e4d720

Browse files
committed
fix(validation): pass whole result to error
1 parent f2480cb commit 1e4d720

6 files changed

Lines changed: 24 additions & 32 deletions

File tree

src/handler.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,13 @@ import type {
1313
EventHandlerWithFetch,
1414
} from "./types/handler.ts";
1515
import type {
16-
InferOutput,
1716
StandardSchemaV1,
17+
FailureResult,
18+
InferOutput,
1819
} from "./utils/internal/standard-schema.ts";
1920
import type { TypedRequest } from "fetchdts";
2021
import type { ErrorDetails } from "./error.ts";
21-
import {
22-
type ValidateIssues,
23-
validatedRequest,
24-
validatedURL,
25-
} from "./utils/internal/validate.ts";
22+
import { validatedRequest, validatedURL } from "./utils/internal/validate.ts";
2623

2724
// --- event handler ---
2825

@@ -69,7 +66,7 @@ export function defineValidatedHandler<
6966
headers?: RequestHeaders;
7067
query?: RequestQuery;
7168
onError?: (
72-
issues: ValidateIssues,
69+
result: FailureResult,
7370
source: "headers" | "body" | "query",
7471
) => ErrorDetails;
7572
};

src/utils/body.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
import { type ErrorDetails, HTTPError } from "../error.ts";
2-
import {
3-
type ValidateIssues,
4-
type ValidateError,
5-
validateData,
6-
} from "./internal/validate.ts";
2+
import { type ValidateError, validateData } from "./internal/validate.ts";
73
import { parseURLEncodedBody } from "./internal/body.ts";
84

95
import type { H3Event } from "../event.ts";
106
import type { InferEventInput } from "../types/handler.ts";
117
import type { ValidateResult } from "./internal/validate.ts";
128
import type {
139
StandardSchemaV1,
10+
FailureResult,
1411
InferOutput,
1512
} from "./internal/standard-schema.ts";
1613

@@ -59,7 +56,7 @@ export async function readValidatedBody<
5956
>(
6057
event: Event,
6158
validate: S,
62-
options?: { onError?: (issues: ValidateIssues) => ErrorDetails },
59+
options?: { onError?: (result: FailureResult) => ErrorDetails },
6360
): Promise<InferOutput<S>>;
6461
export async function readValidatedBody<
6562
Event extends H3Event,

src/utils/internal/validate.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { type ErrorDetails, HTTPError } from "../../error.ts";
33
import type { ServerRequest } from "srvx";
44
import type {
55
StandardSchemaV1,
6+
FailureResult,
67
InferOutput,
78
Issue,
89
} from "./standard-schema.ts";
@@ -19,7 +20,7 @@ export type ValidateFunction<
1920
export type ValidateIssues = ReadonlyArray<Issue>;
2021
export type ValidateError =
2122
| (() => ErrorDetails)
22-
| ((issues: ValidateIssues) => ErrorDetails);
23+
| ((result: FailureResult) => ErrorDetails);
2324

2425
/**
2526
* Validates the given data using the provided validation function.
@@ -34,7 +35,7 @@ export async function validateData<Schema extends StandardSchemaV1>(
3435
data: unknown,
3536
fn: Schema,
3637
options?: {
37-
onError?: (issues: ValidateIssues) => ErrorDetails;
38+
onError?: (result: FailureResult) => ErrorDetails;
3839
},
3940
): Promise<InferOutput<Schema>>;
4041
export async function validateData<T>(
@@ -55,7 +56,7 @@ export async function validateData<T>(
5556
const result = await fn["~standard"].validate(data);
5657
if (result.issues) {
5758
const errorDetails = options?.onError
58-
? options.onError(result.issues)
59+
? options.onError(result)
5960
: {
6061
message: "Validation failed",
6162
issues: result.issues,
@@ -98,7 +99,7 @@ export function validatedRequest<
9899
body?: RequestBody;
99100
headers?: RequestHeaders;
100101
onError?: (
101-
issues: ValidateIssues,
102+
result: FailureResult,
102103
source: "headers" | "body",
103104
) => ErrorDetails;
104105
},
@@ -132,7 +133,7 @@ export function validatedRequest<
132133
.then((result) => {
133134
if (result.issues) {
134135
const errorDetails = validate.onError
135-
? validate.onError(result.issues, "body")
136+
? validate.onError(result, "body")
136137
: {
137138
message: "Validation failed",
138139
issues: result.issues,
@@ -158,7 +159,7 @@ export function validatedURL(
158159
url: URL,
159160
validate: {
160161
query?: StandardSchemaV1;
161-
onError?: (issues: ValidateIssues, source: "query") => ErrorDetails;
162+
onError?: (result: FailureResult, source: "query") => ErrorDetails;
162163
},
163164
): URL {
164165
if (!validate.query) {
@@ -183,15 +184,15 @@ function syncValidate<Source extends "headers" | "query", T = unknown>(
183184
type: Source,
184185
data: unknown,
185186
fn: StandardSchemaV1<T>,
186-
onError?: (issues: ValidateIssues, source: Source) => ErrorDetails,
187+
onError?: (result: FailureResult, source: Source) => ErrorDetails,
187188
): T {
188189
const result = fn["~standard"].validate(data);
189190
if (result instanceof Promise) {
190191
throw new TypeError(`Asynchronous validation is not supported for ${type}`);
191192
}
192193
if (result.issues) {
193194
const errorDetails = onError
194-
? onError(result.issues, type)
195+
? onError(result, type)
195196
: {
196197
message: "Validation failed",
197198
issues: result.issues,

src/utils/request.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
import { type ErrorDetails, HTTPError } from "../error.ts";
2-
import {
3-
type ValidateIssues,
4-
type ValidateError,
5-
validateData,
6-
} from "./internal/validate.ts";
2+
import { type ValidateError, validateData } from "./internal/validate.ts";
73
import { parseQuery } from "./internal/query.ts";
84

95
import type {
106
StandardSchemaV1,
7+
FailureResult,
118
InferOutput,
129
} from "./internal/standard-schema.ts";
1310
import type { ValidateResult } from "./internal/validate.ts";
@@ -37,7 +34,7 @@ export function getValidatedQuery<
3734
>(
3835
event: Event,
3936
validate: S,
40-
options?: { onError?: (issues: ValidateIssues) => ErrorDetails },
37+
options?: { onError?: (result: FailureResult) => ErrorDetails },
4138
): Promise<InferOutput<S>>;
4239
export function getValidatedQuery<
4340
Event extends H3Event,
@@ -143,7 +140,7 @@ export function getValidatedRouterParams<
143140
validate: S,
144141
options?: {
145142
decode?: boolean;
146-
onError?: (issues: ValidateIssues) => ErrorDetails;
143+
onError?: (result: FailureResult) => ErrorDetails;
147144
},
148145
): Promise<InferOutput<S>>;
149146
export function getValidatedRouterParams<

test/handler.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ describe("handler.ts", () => {
114114
query: z.object({
115115
id: z.string().min(3),
116116
}),
117-
onError: (issues, source) => {
117+
onError: ({ issues }, source) => {
118118
return {
119119
status: 500,
120120
statusText: `Custom Zod ${source} validation error`,

test/validate.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ describeMatrix("validate", (t, { it, describe, expect }) => {
7474

7575
t.app.post("/custom-error-zod", async (event) => {
7676
const data = await readValidatedBody(event, zodValidate, {
77-
onError: (issues) => ({
77+
onError: ({ issues }) => ({
7878
status: 500,
7979
statusText: "Custom Zod validation error",
8080
message: summarize(issues),
@@ -226,7 +226,7 @@ describeMatrix("validate", (t, { it, describe, expect }) => {
226226

227227
t.app.get("/custom-error-zod", async (event) => {
228228
const data = await getValidatedQuery(event, zodValidate, {
229-
onError: (issues) => ({
229+
onError: ({ issues }) => ({
230230
status: 500,
231231
statusText: "Custom Zod validation error",
232232
message: summarize(issues),
@@ -335,7 +335,7 @@ describeMatrix("validate", (t, { it, describe, expect }) => {
335335

336336
t.app.get("/custom-error-zod/:id", async (event) => {
337337
const data = await getValidatedRouterParams(event, zodParamValidate, {
338-
onError: (issues) => ({
338+
onError: ({ issues }) => ({
339339
status: 500,
340340
statusText: "Custom Zod validation error",
341341
message: summarize(issues),

0 commit comments

Comments
 (0)