Refresh HiveGrid Cloud reference snapshot

This commit is contained in:
david committed 2026-08-03 15:31:14 +02:00
1 parent 5e99e14772
commit 7f88026f79
5955 files changed
+1177325 -188362

No files matched your search

@@ -33,6 +33,11 @@ export {
export { matchAllowedOrigin, normalizeOrigin } from "./origin.js";
export { isHoneypotTriggered } from "./honeypot.js";
export { evaluateConditionalLogic, isFieldVisible, filterVisibleFields, type ConditionalValues } from "./visibility.js";
export {
validateFormValues,
type FormValueValidationCode,
type FormValueValidationError,
} from "./validation.js";
export {
resolveSubmissionTarget,
applyFieldMapping,
@@ -14,7 +14,7 @@ describe("resolveSubmissionTarget — descriptor shape", () => {
});
it("freezes the empty mapping on the unknown-target fallback path", () => {
const target = resolveSubmissionTarget({ targetType: "entity-row" });
const target = resolveSubmissionTarget({ targetType: "legacy-row-target" });
expect(target.type).toBe("generic");
expect(Object.isFrozen(target.fieldMapping)).toBe(true);
expect(target.fieldMapping).toEqual({});
@@ -0,0 +1,82 @@
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();
});
});
@@ -0,0 +1,92 @@
import type { FormDefinition } from "./schema.js";
export type FormValueValidationCode =
| "FIELD_REQUIRED"
| "FIELD_TOO_LONG"
| "FIELD_TOO_SHORT"
| "FIELD_INVALID_EMAIL"
| "INVALID_PAYLOAD_SHAPE";
export type FormValueValidationError = {
readonly code: FormValueValidationCode;
readonly params: Readonly<Record<string, string | number | boolean>>;
};
/**
* Applies the server-side value rules from a parsed form definition.
*
* Public forms and authenticated portal forms deliberately share this pure
* validator so a portal submission cannot bypass the builder contract.
*/
export function validateFormValues(
values: Readonly<Record<string, unknown>>,
definition: FormDefinition,
): FormValueValidationError | null {
for (const field of definition.fields) {
const raw = values[field.key];
if (field.type === "honeypot") continue;
const params = { fieldKey: field.key, fieldLabel: field.label };
const filled = Array.isArray(raw) ? raw.length > 0 : raw !== undefined && raw !== null && raw !== "" && raw !== false;
if (field.required && !filled) return { code: "FIELD_REQUIRED", params };
if (!filled) continue;
if (typeof raw === "string") {
const maxLength = field.validationRules?.maxLength ?? field.maxLength;
const minLength = field.validationRules?.minLength ?? field.minLength;
if (maxLength !== undefined && raw.length > maxLength) {
return { code: "FIELD_TOO_LONG", params: { ...params, maxLength } };
}
if (minLength !== undefined && raw.length < minLength) {
return { code: "FIELD_TOO_SHORT", params: { ...params, minLength } };
}
if (field.type === "email" && !/^[^\s@]+@[^\s@]+\.[^\s@]+$/u.test(raw)) {
return { code: "FIELD_INVALID_EMAIL", params };
}
if (field.type === "phone" && !/^[+()\d\s./-]{3,40}$/u.test(raw)) {
return { code: "INVALID_PAYLOAD_SHAPE", params };
}
const pattern = field.validationRules?.pattern;
if (pattern) {
try {
if (!new RegExp(pattern, "u").test(raw)) return { code: "INVALID_PAYLOAD_SHAPE", params };
} catch {
return { code: "INVALID_PAYLOAD_SHAPE", params };
}
}
}
if (field.type === "number") {
const numberValue = typeof raw === "number" ? raw : typeof raw === "string" ? Number(raw) : Number.NaN;
if (!Number.isFinite(numberValue)) return { code: "INVALID_PAYLOAD_SHAPE", params };
if (field.validationRules?.min !== undefined && numberValue < field.validationRules.min) {
return { code: "INVALID_PAYLOAD_SHAPE", params };
}
if (field.validationRules?.max !== undefined && numberValue > field.validationRules.max) {
return { code: "INVALID_PAYLOAD_SHAPE", params };
}
}
if (field.type === "select") {
if (typeof raw !== "string" || !field.options?.some((option) => option.value === raw)) {
return { code: "INVALID_PAYLOAD_SHAPE", params };
}
}
if (field.type === "multiselect") {
if (!Array.isArray(raw) || raw.some((value) => typeof value !== "string" || !field.options?.some((option) => option.value === value))) {
return { code: "INVALID_PAYLOAD_SHAPE", params };
}
if (field.validationRules?.minSelected !== undefined && raw.length < field.validationRules.minSelected) {
return { code: "FIELD_REQUIRED", params };
}
if (field.validationRules?.maxSelected !== undefined && raw.length > field.validationRules.maxSelected) {
return { code: "INVALID_PAYLOAD_SHAPE", params };
}
}
if (field.type === "boolean" && typeof raw !== "boolean") {
return { code: "INVALID_PAYLOAD_SHAPE", params };
}
}
return null;
}