import { describe, expect, it } from "vitest"; import { FormDefinitionSchema } from "./schema.js"; import { validateFormValues } from "./validation.js"; const definition = FormDefinitionSchema.parse({ title: "Reklamation", fields: [ { key: "subject", type: "text", label: "Betreff", required: true, validationRules: { minLength: 3, maxLength: 20 } }, { key: "email", type: "email", label: "E-Mail" }, { key: "kind", type: "select", label: "Art", options: [{ value: "quality", label: "Qualität" }] }, { key: "areas", type: "multiselect", label: "Bereiche", options: [{ value: "roof", label: "Dach" }], validationRules: { maxSelected: 1 } }, ], }); const rulesDefinition = FormDefinitionSchema.parse({ title: "Technische Prüfung", fields: [ { key: "trap", type: "honeypot", label: "Leer lassen" }, { key: "summary", type: "text", label: "Zusammenfassung", validationRules: { minLength: 3, maxLength: 8 } }, { key: "phone", type: "phone", label: "Telefon" }, { key: "reference", type: "text", label: "Referenz", validationRules: { pattern: "^[a-z]+$" } }, { key: "brokenPattern", type: "text", label: "Defektes Muster", validationRules: { pattern: "[" } }, { key: "quantity", type: "number", label: "Menge", validationRules: { min: 1, max: 10 } }, { key: "kind", type: "select", label: "Art", options: [{ value: "quality", label: "Qualität" }] }, { key: "areas", type: "multiselect", label: "Bereiche", options: [ { value: "roof", label: "Dach" }, { value: "wall", label: "Wand" }, { value: "floor", label: "Boden" }, ], validationRules: { minSelected: 2, maxSelected: 2 }, }, { key: "confirmed", type: "boolean", label: "Bestätigt" }, ], }); describe("validateFormValues", () => { it("accepts values conforming to the shared builder definition", () => { expect(validateFormValues({ subject: "Mangel", email: "kunde@example.de", kind: "quality", areas: ["roof"] }, definition)).toBeNull(); }); it("rejects missing, malformed and unknown option values", () => { expect(validateFormValues({}, definition)?.code).toBe("FIELD_REQUIRED"); expect(validateFormValues({ subject: "Mangel", email: "x" }, definition)?.code).toBe("FIELD_INVALID_EMAIL"); expect(validateFormValues({ subject: "Mangel", kind: "other" }, definition)?.code).toBe("INVALID_PAYLOAD_SHAPE"); expect(validateFormValues({ subject: "Mangel", areas: ["wall"] }, definition)?.code).toBe("INVALID_PAYLOAD_SHAPE"); }); it("enforces string length, phone and pattern rules", () => { expect(validateFormValues({ summary: "ab" }, rulesDefinition)).toMatchObject({ code: "FIELD_TOO_SHORT", params: { minLength: 3 } }); expect(validateFormValues({ summary: "zu ausführlich" }, rulesDefinition)).toMatchObject({ code: "FIELD_TOO_LONG", params: { maxLength: 8 }, }); expect(validateFormValues({ phone: "not-a-phone" }, rulesDefinition)?.code).toBe("INVALID_PAYLOAD_SHAPE"); expect(validateFormValues({ reference: "ABC" }, rulesDefinition)?.code).toBe("INVALID_PAYLOAD_SHAPE"); expect(validateFormValues({ brokenPattern: "anything" }, rulesDefinition)?.code).toBe("INVALID_PAYLOAD_SHAPE"); }); it("enforces number bounds and payload shape", () => { expect(validateFormValues({ quantity: "many" }, rulesDefinition)?.code).toBe("INVALID_PAYLOAD_SHAPE"); expect(validateFormValues({ quantity: 0 }, rulesDefinition)?.code).toBe("INVALID_PAYLOAD_SHAPE"); expect(validateFormValues({ quantity: 11 }, rulesDefinition)?.code).toBe("INVALID_PAYLOAD_SHAPE"); expect(validateFormValues({ quantity: 4 }, rulesDefinition)).toBeNull(); }); it("enforces select, multiselect and boolean payloads", () => { expect(validateFormValues({ kind: 1 }, rulesDefinition)?.code).toBe("INVALID_PAYLOAD_SHAPE"); expect(validateFormValues({ areas: "roof" }, rulesDefinition)?.code).toBe("INVALID_PAYLOAD_SHAPE"); expect(validateFormValues({ areas: ["roof"] }, rulesDefinition)?.code).toBe("FIELD_REQUIRED"); expect(validateFormValues({ areas: ["roof", "wall", "floor"] }, rulesDefinition)?.code).toBe("INVALID_PAYLOAD_SHAPE"); expect(validateFormValues({ confirmed: "yes" }, rulesDefinition)?.code).toBe("INVALID_PAYLOAD_SHAPE"); }); it("ignores honeypot values in the shared business-field validator", () => { expect(validateFormValues({ trap: { injected: true } }, rulesDefinition)).toBeNull(); }); });