import { z } from 'zod'; export const templateSchema = z.object({ "templateId": z.string().regex(new RegExp("^[a-zA-Z0-9_-]+$")).min(1).max(100).describe("ID of the template to use (original format)").optional(), "templatePath": z.string().regex(new RegExp("^/.*")).min(1).max(500).describe("MinIO path to template file (processed format)").optional(), "variables": z.record(z.any().superRefine((x, ctx) => { const schemas = [z.string().min(1).max(1000).describe("Simple text variable"), z.object({ "type": z.literal("image").describe("Image replacement"), "src": z.any().superRefine((x, ctx) => { const schemas = [z.any().describe("Local Storage image path from file upload (must start with /image/)"), z.any().describe("Public HTTP/HTTPS URL")]; const errors = schemas.reduce( (errors, schema) => ((result) => result.error ? [...errors, result.error] : errors)( schema.safeParse(x), ), [], ); if (schemas.length - errors.length !== 1) { ctx.addIssue({ path: ctx.path, code: "invalid_union", unionErrors: errors, message: "Invalid input: Should pass single schema", }); } }).describe("Image file path (storage URL starting with /image/ or public HTTP/HTTPS URL)"), "alt": z.string().max(200).describe("Alternative text for accessibility").optional(), "width": z.number().gte(10).lte(2000).describe("Image width in pixels").optional(), "height": z.number().gte(10).lte(2000).describe("Image height in pixels").optional() }).strict().describe("Image to replace placeholder"), z.object({ "type": z.literal("table").describe("Dynamic table data"), "data": z.array(z.record(z.any()).describe("Row data as key-value pairs matching template placeholders")).min(1).max(100).describe("Array of row objects with properties matching template column placeholders") }).strict().describe("Table data to populate template table rows"), z.object({ "type": z.literal("list").describe("List of items"), "items": z.array(z.string().min(1).max(1000)).min(1).max(50).describe("List items to insert") }).strict().describe("List items to insert at placeholder position"), z.object({ "type": z.literal("qrcode").describe("QR code to generate and insert"), "qrType": z.enum(["url","wifi","vcard"]).describe("Type of QR code: url (link), wifi (WiFi credentials), vcard (contact card)"), "data": z.record(z.any()).describe("QR code data (structure depends on qrType)"), "size": z.number().gte(50).lte(1000).describe("QR code size in pixels (width and height, always 1:1 ratio)").default(200), "errorCorrection": z.enum(["L","M","Q","H"]).describe("Error correction level: L (~7%), M (~15%), Q (~25%), H (~30%)").default("M") }).strict().and(z.intersection(z.any(), z.intersection(z.any(), z.any()))).describe("QR code to generate and insert at placeholder position")]; const errors = schemas.reduce( (errors, schema) => ((result) => result.error ? [...errors, result.error] : errors)( schema.safeParse(x), ), [], ); if (schemas.length - errors.length !== 1) { ctx.addIssue({ path: ctx.path, code: "invalid_union", unionErrors: errors, message: "Invalid input: Should pass single schema", }); } })).describe("Variables to replace in the template. Use {{varName}} in template. Supports strings, images, tables, and lists.").optional(), "output": z.object({ "filename": z.string().regex(new RegExp("^[a-zA-Z0-9_-]+$")).min(1).max(100).describe("Override the output filename").optional(), "type": z.enum(["docx","pdf"]).describe("Output document format").default("docx") }).strict().describe("Optional output settings to override template defaults").optional() }).strict().and(z.union([z.any().refine((value) => !z.any().safeParse(value).success, "Invalid input: Should NOT be valid against schema"), z.any().refine((value) => !z.any().safeParse(value).success, "Invalid input: Should NOT be valid against schema")])).describe("JSON schema for generating documents from templates by replacing variables")