"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateJsonField = void 0;
const lodash_1 = require("lodash");
const zod_1 = require("zod");
const validateJsonField = (schema, errorMessage) => {
    return (str, ctx) => {
        if ((0, lodash_1.isNil)(str) || (0, lodash_1.isEmpty)(str))
            return undefined;
        try {
            const parsedJson = JSON.parse(str);
            if ((0, lodash_1.isEmpty)(parsedJson)) {
                return undefined;
            }
            const result = schema.safeParse(parsedJson);
            if (!result.success) {
                ctx.addIssue({
                    code: 'custom',
                    message: errorMessage,
                });
                return zod_1.z.NEVER;
            }
            return parsedJson;
        }
        catch (e) {
            ctx.addIssue({ code: 'custom', message: 'Invalid JSON.' });
            return zod_1.z.NEVER;
        }
    };
};
exports.validateJsonField = validateJsonField;
//# sourceMappingURL=validateJsonField.js.map