@wthek/zod-transformer
Version:
Framework-agnostic utility that transforms Zod validation errors into structured http-error-kit errors. Preserves issue details and integrates cleanly with custom formatters.
28 lines (27 loc) • 1.02 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.KitZodTransformer = KitZodTransformer;
var zod_1 = require("zod");
var http_error_kit_1 = require("http-error-kit");
var http_response_status_code_1 = require("http-response-status-code");
/**
* Transforms a ZodError into a structured KitHttpError with a BadRequest status.
* If the provided error is not an instance of ZodError, it is thrown unchanged.
*
* @param {unknown} error The error to check and transform. If it is not a ZodError, it is thrown as-is.
* @returns {unknown} The transformed error.
* @throws {unknown} If the error is not a ZodError, it is thrown as-is.
*/
function KitZodTransformer(error) {
try {
if (error instanceof zod_1.ZodError) {
throw new http_error_kit_1.KitHttpError(http_response_status_code_1.BAD_REQUEST, "Zod Validation Failed", {
issues: error.issues,
});
}
}
catch (err) {
throw err;
}
throw error;
}