"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateParams = exports.validateQuery = exports.validateBody = void 0;
const apiResponse_js_1 = require("../utils/apiResponse.js");
const validate = (source, schema) => {
    return (req, res, next) => {
        const result = schema.safeParse(req[source]);
        if (!result.success) {
            return (0, apiResponse_js_1.sendError)(res, 400, "VALIDATION_ERROR", `Invalid request ${source}.`, result.error.flatten());
        }
        if (source === "body")
            req.validatedBody = result.data;
        if (source === "query")
            req.validatedQuery = result.data;
        if (source === "params")
            req.validatedParams = result.data;
        next();
    };
};
const validateBody = (schema) => validate("body", schema);
exports.validateBody = validateBody;
const validateQuery = (schema) => validate("query", schema);
exports.validateQuery = validateQuery;
const validateParams = (schema) => validate("params", schema);
exports.validateParams = validateParams;
//# sourceMappingURL=validateRequest.js.map