BREAKING CHANGE(request): introduce lazy request body parsing via ctx.json()/text()/arrayBuffer()/formData and remove IRequestContext.body
This commit is contained in:
@@ -116,15 +116,15 @@ export function createValidationErrorResponse(
|
||||
* Validate the full request based on OpenAPI metadata
|
||||
* Returns a Response if validation fails, undefined if valid
|
||||
*/
|
||||
export function validateRequest(
|
||||
export async function validateRequest(
|
||||
ctx: IRequestContext,
|
||||
openapi: IOpenApiRouteMeta
|
||||
): {
|
||||
): Promise<{
|
||||
valid: boolean;
|
||||
response?: Response;
|
||||
coercedParams?: Record<string, unknown>;
|
||||
coercedQuery?: Record<string, unknown>;
|
||||
} {
|
||||
}> {
|
||||
const allErrors: Array<{ errors: IValidationError[]; source: string }> = [];
|
||||
|
||||
// Coerce and validate path parameters
|
||||
@@ -164,10 +164,16 @@ export function validateRequest(
|
||||
}
|
||||
}
|
||||
|
||||
// Validate request body
|
||||
// Validate request body (lazy parsing via ctx.json())
|
||||
if (openapi.requestBody) {
|
||||
const required = openapi.requestBody.required !== false;
|
||||
const body = ctx.body;
|
||||
let body: unknown;
|
||||
|
||||
try {
|
||||
body = await ctx.json();
|
||||
} catch {
|
||||
body = undefined;
|
||||
}
|
||||
|
||||
if (required && (body === undefined || body === null)) {
|
||||
allErrors.push({
|
||||
@@ -210,7 +216,7 @@ export function validateRequest(
|
||||
*/
|
||||
export function createValidationInterceptor(openapi: IOpenApiRouteMeta) {
|
||||
return async (ctx: IRequestContext): Promise<IRequestContext | Response | void> => {
|
||||
const result = validateRequest(ctx, openapi);
|
||||
const result = await validateRequest(ctx, openapi);
|
||||
|
||||
if (!result.valid && result.response) {
|
||||
return result.response;
|
||||
|
||||
Reference in New Issue
Block a user