Skip to content

Commit 761ee09

Browse files
committed
Addressed feedbacks
1 parent aebbfbb commit 761ee09

File tree

2 files changed

+54
-29
lines changed

2 files changed

+54
-29
lines changed

packages/event-handler/src/http/middleware/validation.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,9 @@ export const createValidationMiddleware = <
3636
if (reqSchemas.body) {
3737
const clonedRequest = reqCtx.req.clone();
3838
const contentType = reqCtx.req.headers.get('content-type');
39-
let bodyData: unknown;
40-
41-
if (contentType?.includes('application/json')) {
42-
try {
43-
bodyData = await clonedRequest.json();
44-
} catch {
45-
// If JSON parsing fails, get as text and let validator handle it
46-
bodyData = await reqCtx.req.clone().text();
47-
}
48-
} else {
49-
bodyData = await clonedRequest.text();
50-
}
39+
const bodyData = contentType?.includes('application/json')
40+
? await clonedRequest.json()
41+
: await clonedRequest.text();
5142

5243
const validatedBody = await validateRequest(
5344
reqSchemas.body,

packages/event-handler/src/types/http.ts

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -291,34 +291,68 @@ type RouterResponse =
291291
| ALBResult;
292292

293293
/**
294-
* Configuration for request validation
294+
* Configuration for request validation.
295+
* At least one of body, headers, path, or query must be provided.
295296
*/
296-
type RequestValidationConfig<T = unknown> = {
297-
body?: StandardSchemaV1<unknown, T>;
298-
headers?: StandardSchemaV1<unknown, Record<string, string>>;
299-
path?: StandardSchemaV1<unknown, Record<string, string>>;
300-
query?: StandardSchemaV1<unknown, Record<string, string>>;
301-
};
297+
type RequestValidationConfig<T = unknown> =
298+
| {
299+
body: StandardSchemaV1<unknown, T>;
300+
headers?: StandardSchemaV1<unknown, Record<string, string>>;
301+
path?: StandardSchemaV1<unknown, Record<string, string>>;
302+
query?: StandardSchemaV1<unknown, Record<string, string>>;
303+
}
304+
| {
305+
body?: StandardSchemaV1<unknown, T>;
306+
headers: StandardSchemaV1<unknown, Record<string, string>>;
307+
path?: StandardSchemaV1<unknown, Record<string, string>>;
308+
query?: StandardSchemaV1<unknown, Record<string, string>>;
309+
}
310+
| {
311+
body?: StandardSchemaV1<unknown, T>;
312+
headers?: StandardSchemaV1<unknown, Record<string, string>>;
313+
path: StandardSchemaV1<unknown, Record<string, string>>;
314+
query?: StandardSchemaV1<unknown, Record<string, string>>;
315+
}
316+
| {
317+
body?: StandardSchemaV1<unknown, T>;
318+
headers?: StandardSchemaV1<unknown, Record<string, string>>;
319+
path?: StandardSchemaV1<unknown, Record<string, string>>;
320+
query: StandardSchemaV1<unknown, Record<string, string>>;
321+
};
302322

303323
/**
304-
* Configuration for response validation
324+
* Configuration for response validation.
325+
* At least one of body or headers must be provided.
305326
*/
306-
type ResponseValidationConfig<T extends HandlerResponse = HandlerResponse> = {
307-
body?: StandardSchemaV1<HandlerResponse, T>;
308-
headers?: StandardSchemaV1<Record<string, string>, Record<string, string>>;
309-
};
327+
type ResponseValidationConfig<T extends HandlerResponse = HandlerResponse> =
328+
| {
329+
body: StandardSchemaV1<HandlerResponse, T>;
330+
headers?: StandardSchemaV1<
331+
Record<string, string>,
332+
Record<string, string>
333+
>;
334+
}
335+
| {
336+
body?: StandardSchemaV1<HandlerResponse, T>;
337+
headers: StandardSchemaV1<Record<string, string>, Record<string, string>>;
338+
};
310339

311340
/**
312341
* Validation configuration for request and response.
313-
* At least one of req or res should be provided.
342+
* At least one of req or res must be provided.
314343
*/
315344
type ValidationConfig<
316345
TReqBody = unknown,
317346
TResBody extends HandlerResponse = HandlerResponse,
318-
> = {
319-
req?: RequestValidationConfig<TReqBody>;
320-
res?: ResponseValidationConfig<TResBody>;
321-
};
347+
> =
348+
| {
349+
req: RequestValidationConfig<TReqBody>;
350+
res?: ResponseValidationConfig<TResBody>;
351+
}
352+
| {
353+
req?: RequestValidationConfig<TReqBody>;
354+
res: ResponseValidationConfig<TResBody>;
355+
};
322356

323357
/**
324358
* Validation error details

0 commit comments

Comments
 (0)