Files
archie/ui/src/settings/validation.ts
T
sam b701f60a99 feat(scheduling): schedules and runs in the State Store with grid timing and run history
Schedules leave the schedules control-plane resource for State Store tables
behind a ScheduleService contract. A run is claimed per schedule slot, missed
slots fire once, the next run stays on the schedule's grid, cron is read in the
schedule-settings time zone, and failed runs are recorded and advance. Three
failures in a row raise a schedule.failing alert. A fired run records a
schedule-fired capture. Run history is pruned by age and count.

Removes the chat and spawn-task runners, the dangerous-payload guard and the
old schedules settings page.
2026-10-26 00:49:24 +11:00

104 lines
4.0 KiB
TypeScript

import { parseGoDuration } from "../components/ui/duration-input/duration.ts";
/**
* Client-side validation for the settings drafts.
*
* The server validates every replace, so this is not the only line of
* defence: it exists so Save can be refused before the round-trip and the
* operator sees the reason beside the field. The rules mirror the server's
* (internal/infrastructure/controlplanestore), and the same map is read by the store, the
* save bar and the pages' inline messages, so a rule is written once.
*
* A hand-written mirror drifts, which is why every rule here is pinned to the
* server's by internal/infrastructure/controlplanestore/testdata/dashboard-validators.json:
* a Go test runs the server validators over that fixture and this file's
* tests run these validators over the same cases, so changing one side alone
* fails a test. Add a case to the fixture, not to one side only.
*/
/** One field that must be fixed before its section can be saved. `path` is the
* dotted leaf path diffValues reports, so an issue lines up with its change. */
export interface FieldIssue {
path: string;
label: string;
message: string;
}
/** A section's rule: the draft value in, everything blocking a save out. */
export type DraftValidator = (value: unknown) => FieldIssue[];
/** An issue with the section it was found in. */
export interface BlockingField extends FieldIssue {
kind: string;
}
/**
* collectIssues runs each dirty section's validator against its draft. It is
* driven by the dirty list on purpose: a section with no changes is not
* blocked, so a value the server already accepted can never stop an unrelated
* save.
*/
export function collectIssues(
dirtyKinds: readonly string[],
draftValue: (kind: string) => unknown,
validators: Record<string, DraftValidator | undefined>,
): BlockingField[] {
return dirtyKinds.flatMap((kind) => {
const validate = validators[kind];
if (!validate) return [];
return validate(draftValue(kind)).map((issue) => ({ ...issue, kind }));
});
}
interface SchedulingDraft {
poll_interval?: string;
label?: string | null;
dispatch?: { trigger?: string };
}
/** validateSchedulingPolicy mirrors validateScheduling: a positive interval
* and the label a label/either trigger requires. */
export function validateSchedulingPolicy(value: unknown): FieldIssue[] {
const policy = (value ?? {}) as SchedulingDraft;
const issues: FieldIssue[] = [];
const ms = parseGoDuration(policy.poll_interval ?? "");
if (ms === null || ms <= 0)
issues.push({
path: "poll_interval",
label: "Poll interval",
message: "Must be longer than zero.",
});
const trigger = policy.dispatch?.trigger;
if (trigger && trigger !== "assignee" && !policy.label?.trim())
issues.push({
path: "label",
label: "Label",
message: "Required when the trigger is label or either.",
});
return issues;
}
const MODEL_ALIAS_NAME = /^[a-z0-9][a-z0-9._-]*$/;
/** validateModelAliases mirrors the server's validateModelAliases: alias
* names are lowercase tokens, each value splits on its first slash into a
* non-empty provider and model. The instance table's default alias is
* checked on save by the server, which knows whose table it is. */
export function validateModelAliases(value: unknown): FieldIssue[] {
const aliases = (value ?? {}) as Record<string, string>;
return Object.entries(aliases).flatMap(([alias, model]): FieldIssue[] => {
if (!MODEL_ALIAS_NAME.test(alias))
return [{ path: alias, label: alias || "Alias", message: "Use lowercase letters, digits, '.', '_' or '-'." }];
const slash = String(model).indexOf("/");
if (slash <= 0 || slash === String(model).length - 1)
return [{ path: alias, label: alias, message: "Use provider/model." }];
return [];
});
}
/** The rules the store applies to a dirty draft, keyed by resource kind. */
export const draftValidators: Record<string, DraftValidator> = {
"scheduling-policy": validateSchedulingPolicy,
"model-aliases": validateModelAliases,
};