UNPKG

@noony-serverless/core

Version:

A Middy base framework compatible with Firebase and GCP Cloud Functions with TypeScript

98 lines 3.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.bodyValidatorMiddleware = exports.BodyValidationMiddleware = void 0; const zod_1 = require("zod"); const errors_1 = require("../core/errors"); const validateBody = async (schema, data) => { try { return await schema.parseAsync(data); } catch (error) { if (error instanceof zod_1.z.ZodError) { throw new errors_1.ValidationError('Validation error', error.issues); } throw error; } }; /** * Body validation middleware using Zod schemas for runtime type checking. * Validates the parsed request body against a provided Zod schema and sets * the validated result in context.req.validatedBody. * * @template T - The expected type of the validated body data * @implements {BaseMiddleware} * * @example * Simple user creation with type safety: * ```typescript * import { z } from 'zod'; * import { Handler, BodyValidationMiddleware } from '@noony-serverless/core'; * * const userSchema = z.object({ * name: z.string().min(1), * email: z.string().email(), * age: z.number().min(18) * }); * * type UserRequest = z.infer<typeof userSchema>; * * async function handleCreateUser(context: Context<UserRequest, AuthenticatedUser>) { * const user = context.req.validatedBody!; // Fully typed * return { success: true, user: { id: '123', ...user } }; * } * * const createUserHandler = new Handler<UserRequest, AuthenticatedUser>() * .use(new BodyValidationMiddleware<UserRequest, AuthenticatedUser>(userSchema)) * .handle(handleCreateUser); * ``` */ class BodyValidationMiddleware { schema; constructor(schema) { this.schema = schema; } async before(context) { context.req.validatedBody = await validateBody(this.schema, context.req.parsedBody); } } exports.BodyValidationMiddleware = BodyValidationMiddleware; /** * Factory function that creates a body validation middleware with Zod schema validation. * This function validates and parses the request body, setting the result in context.req.parsedBody. * * @template T - The expected type of the validated body data * @param schema - Zod schema to validate against * @returns A BaseMiddleware object with validation logic * * @example * Simple login validation: * ```typescript * import { z } from 'zod'; * import { Handler, bodyValidatorMiddleware } from '@noony-serverless/core'; * * const loginSchema = z.object({ * username: z.string().min(3), * password: z.string().min(8) * }); * * type LoginRequest = z.infer<typeof loginSchema>; * * async function handleLogin(context: Context<LoginRequest, AuthenticatedUser>) { * const credentials = context.req.parsedBody as LoginRequest; * const token = await authenticate(credentials.username, credentials.password); * return { success: true, token }; * } * * const loginHandler = new Handler<LoginRequest, AuthenticatedUser>() * .use(bodyValidatorMiddleware<LoginRequest, AuthenticatedUser>(loginSchema)) * .handle(handleLogin); * ``` */ // Modified to fix type instantiation error const bodyValidatorMiddleware = (schema) => ({ before: async (context) => { context.req.parsedBody = await validateBody(schema, context.req.body); }, }); exports.bodyValidatorMiddleware = bodyValidatorMiddleware; //# sourceMappingURL=bodyValidationMiddleware.js.map