Files
triggershell/app/src/lib/validation/variable-schema.ts
T

58 lines
1.7 KiB
TypeScript
Raw Normal View History

2026-08-15 18:37:30 +02:00
import { z } from "zod";
import type { ScriptConfig, VariableConfig } from "../config/schema";
function fieldSchema(variable: VariableConfig): z.ZodTypeAny {
let field: z.ZodTypeAny;
switch (variable.type) {
case "string": {
let s = z.string();
if (variable.minLength !== undefined) s = s.min(variable.minLength);
if (variable.maxLength !== undefined) s = s.max(variable.maxLength);
if (variable.pattern) s = s.regex(new RegExp(variable.pattern));
field = s;
break;
}
case "number": {
let n = z.number();
if (variable.min !== undefined) n = n.min(variable.min);
if (variable.max !== undefined) n = n.max(variable.max);
field = n;
break;
}
case "boolean":
field = z.boolean();
break;
case "enum":
field = z.enum(variable.choices as [string, ...string[]]);
break;
case "multiselect":
field = z.array(z.enum(variable.choices as [string, ...string[]]));
break;
}
if (!variable.required) {
field = field.optional().or(z.literal(""));
}
return field;
}
/** Builds one Zod object schema from a list of variable definitions - the single source of truth
* imported by both the client form resolver and the server-side run-creation handler. */
export function buildVariableSchemaFromList(
variables: readonly VariableConfig[],
) {
const shape: Record<string, z.ZodTypeAny> = {};
for (const variable of variables) {
shape[variable.name] = fieldSchema(variable);
}
return z.object(shape);
}
export function buildVariableSchema(script: ScriptConfig) {
return buildVariableSchemaFromList(script.variables);
}
export type VariableValues = Record<string, unknown>;