exstack
Version:
A utility library designed to simplify and enhance Express.js applications.
85 lines (83 loc) • 3.22 kB
JavaScript
const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
const require_http_error = require('./helps/http-error.cjs');
let zod = require("zod");
zod = require_rolldown_runtime.__toESM(zod);
//#region src/zod.ts
/**
* Express.js-compatible validator class.
*/
var Validator = class {
/**
* Core validation logic used internally by all public methods.
* @template T - The part of the request to validate ('body' | 'query' | 'params' | 'all')
* @param {T} target - The target request part or 'all'
* @param {Schema<T>} schema - Zod schema(s) for validation
* @returns {RequestHandler} Express middleware
* @private
*/
#middleware = (target, schema) => (req, _, next) => {
try {
if (target === "all") {
const schemas = schema;
for (const key of Object.keys(schemas)) {
const zodSchema = schemas[key];
if (!zodSchema) continue;
const result = zodSchema.safeParse(req[key] || {});
if (!result.success) throw new require_http_error.HttpError(400, {
code: "VALIDATION_ERROR",
data: (0, zod.flattenError)(result.error),
cause: (0, zod.prettifyError)(result.error),
message: `Invalid data in req.${key}`
});
req._validated ??= {};
req._validated[key] = result.data;
}
} else {
const result = schema.safeParse(req[target] || {});
if (!result.success) throw new require_http_error.HttpError(400, {
code: "VALIDATION_ERROR",
data: (0, zod.flattenError)(result.error),
cause: (0, zod.prettifyError)(result.error),
message: `Invalid data in req.${target}`
});
req._validated ??= {};
req._validated[target] = result.data;
}
req.valid = (t) => req._validated?.[t];
next();
} catch (error) {
next(error);
}
};
/**
* Validates the request body against a Zod schema.
* @param {ZodType} schema - Zod schema to validate `req.body`
* @returns {RequestHandler} Middleware that validates `req.body` and populates `req._validated.body`
*/
body = (schema) => this.#middleware("body", schema);
/**
* Validates the request query string against a Zod schema.
* @param {ZodType} schema - Zod schema to validate `req.query`
* @returns {RequestHandler} Middleware that validates `req.query` and populates `req._validated.query`
*/
query = (schema) => this.#middleware("query", schema);
/**
* Validates the request URL parameters against a Zod schema.
* @param {ZodType} schema - Zod schema to validate `req.params`
* @returns {RequestHandler} Middleware that validates `req.params` and populates `req._validated.params`
*/
params = (schema) => this.#middleware("params", schema);
/**
* Validates multiple parts of the request (body, query, params) simultaneously.
* @param {Schema<'all'>} schema - Object containing Zod schemas for each request part
* @returns {RequestHandler} Middleware that validates all provided request parts and populates `req._validated`
*/
all = (schema) => this.#middleware("all", schema);
};
/**
* Provides middleware for validating request body, query, params, or all of them using Zod schemas.
* Attaches a `req.valid()` helper to access validated data.
*/
const validator = new Validator();
//#endregion
exports.validator = validator;