Files
archie/ui/src/settings/validation.ts
T
sam 656d21520f fix(webui): pin the dashboard validators to the control plane's
ui/src/settings/validation.ts hand-mirrors three control-plane validators with
no shared source, and the model-role rule had already diverged: the dashboard
required a stricter regex while the server only asks for a slash, so it refused
values the server accepts.

Add a shared fixture (internal/app/controlplane/testdata/dashboard-validators.json)
that both sides run through their real implementations: a Go test over the
server validators and ui/test/control-plane-validator-parity.test.ts over the
dashboard ones. Either side changing a rule alone now fails the other's test.

Fix the diverged rule to mirror the server's predicate exactly (non-blank role,
model containing a slash), so an empty or whitespace-padded model is judged the
same way the control plane judges it. Regenerate ui/dist and the dev-docs
artifact.
2026-10-02 11:59:17 +10:00

127 lines
4.6 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/app/controlplane), 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/app/controlplane/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 (archie-core-ui-dashboard-3). 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;
}
interface ScheduleDraft {
id?: string;
}
/** validateSchedules mirrors validateSchedules: an ID on every job, and no two
* jobs sharing one. */
export function validateSchedules(value: unknown): FieldIssue[] {
const jobs = Array.isArray(value) ? (value as ScheduleDraft[]) : [];
const issues: FieldIssue[] = [];
const seen = new Set<string>();
jobs.forEach((job, i) => {
const id = job.id?.trim() ?? "";
if (!id) {
issues.push({ path: `${i}.id`, label: `Schedule ${i + 1}`, message: "ID is required." });
return;
}
if (seen.has(id))
issues.push({ path: `${i}.id`, label: `Schedule ${i + 1}`, message: `Duplicate schedule "${id}".` });
seen.add(id);
});
return issues;
}
/** validateModelRoles mirrors validateModelRoles exactly: the server's whole
* predicate is `strings.TrimSpace(role) == "" || !strings.Contains(model,
* "/")`. It deliberately does not impose a stricter shape, such as requiring
* a non-space provider before the slash -- the dashboard refusing a value the
* server would accept blocks a legitimate save. */
export function validateModelRoles(value: unknown): FieldIssue[] {
const roles = (value ?? {}) as Record<string, string>;
return Object.entries(roles)
.filter(([role, model]) => !role.trim() || !String(model).includes("/"))
.map(([role]) => ({
path: role,
label: role.trim() || "Role",
message: role.trim() ? "Use provider/model." : "Role name is required.",
}));
}
/** The rules the store applies to a dirty draft, keyed by resource kind. */
export const draftValidators: Record<string, DraftValidator> = {
"scheduling-policy": validateSchedulingPolicy,
schedules: validateSchedules,
"model-role-assignments": validateModelRoles,
};