|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 4 | +exports.validateBody = exports.validateRequest = exports.validateAndSanitize = exports.commonSchemas = void 0; |
| 5 | + |
| 6 | +const zod_1 = require("zod"); |
| 7 | +const errors_js_1 = require("../types/errors.js"); |
| 8 | +const sanitize_js_1 = require("./sanitize.js"); |
| 9 | + |
| 10 | +exports.commonSchemas = { |
| 11 | + uuid: zod_1.z.string().uuid(), |
| 12 | + id: zod_1.z.string().trim().min(1).max(128).regex(/^[a-zA-Z0-9._:-]+$/), |
| 13 | + email: zod_1.z.string().trim().email().max(255).transform((value) => value.toLowerCase()), |
| 14 | + pagination: zod_1.z.object({ |
| 15 | + page: zod_1.z.coerce.number().int().min(1).max(1000).default(1), |
| 16 | + limit: zod_1.z.coerce.number().int().min(1).max(100).default(20), |
| 17 | + }), |
| 18 | + stellarAddress: zod_1.z.string().regex(/^G[A-Z0-9]{55}$/, "Invalid Stellar address format"), |
| 19 | + amount: zod_1.z.coerce.number().positive().finite().max(1_000_000_000), |
| 20 | +}; |
| 21 | + |
| 22 | +function sanitizeValue(value, options) { |
| 23 | + return sanitize_js_1.InputSanitizer.getInstance().sanitize(value, { |
| 24 | + sqlEscape: false, |
| 25 | + htmlSanitization: false, |
| 26 | + escapeHtml: false, |
| 27 | + ...options, |
| 28 | + }); |
| 29 | +} |
| 30 | + |
| 31 | +function parseSchema(schema, value, stripUnknown) { |
| 32 | + if (stripUnknown && schema instanceof zod_1.z.ZodObject) { |
| 33 | + return schema.strip().parse(value ?? {}); |
| 34 | + } |
| 35 | + return schema.parse(value ?? {}); |
| 36 | +} |
| 37 | + |
| 38 | +function formatZodError(error) { |
| 39 | + return error.issues.map((issue) => ({ |
| 40 | + path: issue.path.join(".") || "root", |
| 41 | + message: issue.message, |
| 42 | + code: issue.code, |
| 43 | + })); |
| 44 | +} |
| 45 | + |
| 46 | +function isZodError(error) { |
| 47 | + return error instanceof zod_1.ZodError || |
| 48 | + (typeof error === "object" && |
| 49 | + error !== null && |
| 50 | + error.name === "ZodError" && |
| 51 | + Array.isArray(error.errors)); |
| 52 | +} |
| 53 | + |
| 54 | +function validateAndSanitize(schemas, options = {}) { |
| 55 | + return function validationMiddleware(req, _res, next) { |
| 56 | + try { |
| 57 | + const shouldSanitize = options.sanitize ?? true; |
| 58 | + |
| 59 | + if (schemas.body) { |
| 60 | + const value = shouldSanitize ? sanitizeValue(req.body ?? {}, options.sanitizer) : req.body; |
| 61 | + req.body = parseSchema(schemas.body, value, options.stripUnknown); |
| 62 | + } |
| 63 | + |
| 64 | + if (schemas.query) { |
| 65 | + const value = shouldSanitize ? sanitizeValue(req.query ?? {}, options.sanitizer) : req.query; |
| 66 | + req.query = parseSchema(schemas.query, value, options.stripUnknown); |
| 67 | + } |
| 68 | + |
| 69 | + if (schemas.params) { |
| 70 | + const value = shouldSanitize ? sanitizeValue(req.params ?? {}, options.sanitizer) : req.params; |
| 71 | + req.params = parseSchema(schemas.params, value, options.stripUnknown); |
| 72 | + } |
| 73 | + |
| 74 | + if (schemas.headers) { |
| 75 | + const headers = Object.fromEntries( |
| 76 | + Object.entries(req.headers).map(([key, value]) => [key.toLowerCase(), value]) |
| 77 | + ); |
| 78 | + parseSchema(schemas.headers, headers, options.stripUnknown); |
| 79 | + } |
| 80 | + |
| 81 | + next(); |
| 82 | + } catch (error) { |
| 83 | + if (isZodError(error)) { |
| 84 | + return next(new errors_js_1.AppError( |
| 85 | + 400, |
| 86 | + "Request validation failed", |
| 87 | + "ERR_VALIDATION_FAILED", |
| 88 | + formatZodError(error) |
| 89 | + )); |
| 90 | + } |
| 91 | + |
| 92 | + next(error); |
| 93 | + } |
| 94 | + }; |
| 95 | +} |
| 96 | + |
| 97 | +exports.validateAndSanitize = validateAndSanitize; |
| 98 | +exports.validateRequest = validateAndSanitize; |
| 99 | +const validateBody = (schema, options) => validateAndSanitize({ body: schema }, options); |
| 100 | +exports.validateBody = validateBody; |
| 101 | +exports.default = validateAndSanitize; |
0 commit comments