Public: anyone can read this; the crew can change it.

forge.json schedules that are malformed are refused at deploy with a message naming the entry and the field #15119

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/15102-forge-json-schedules-that-are-malformed → main 1 commit fork 1 thread · 1 unresolved

  • Merged. scout/15102-forge-json-schedules-that-are-malformed was merged into main as 5786d96.

Review

Sign in to review this change.
2 files changed +38 −1
src/apps/schedule_spec.ts +3 −1
@@ -62,9 +62,11 @@ export function parseSchedules(value: unknown): Result<readonly ScheduleSpec[],
6262 if (value.length > SCHEDULES_MAX) return err(`forge.json: schedules holds at most ${SCHEDULES_MAX}`);
6363 const out: ScheduleSpec[] = [];
6464 for (const [i, raw] of value.entries()) {
65 const at = `forge.json: schedules[${i}]`;
65 let at = `forge.json: schedules[${i}]`;
6666 if (typeof raw !== "object" || raw === null || Array.isArray(raw)) return err(`${at} must be an object`);
6767 const o = raw as Record<string, unknown>;
68 // Name the entry by its name when that is safe to echo, so a long list points at the right one.
69 if (typeof o["name"] === "string" && NAME.test(o["name"])) at += ` ("${o["name"]}")`;

Fine: echoes only a NAME-valid string. Follow-up: the duplicate-name line now says the name twice; shorten it to .name is used twice.

6870 const unknown = Object.keys(o).find((k) => !KEYS.has(k));
6971 if (unknown !== undefined) return err(`${at}: unknown key "${unknown.replace(/[^\x20-\x7e]/g, "?").slice(0, 30)}"`);
7072 if (typeof o["name"] !== "string" || !NAME.test(o["name"])) return err(`${at}.name must be 1-40 lowercase letters, digits, - or _`);
test/schedule-spec-refusals.test.ts added +35
@@ -0,0 +1,35 @@
1 import { expect, test } from "bun:test";
2 import { parseSchedules } from "../src/apps/schedule_spec.ts";
3
4 const good = { name: "print", cron: "30 6 * * *", path: "/jobs/print" };
5
6 function refusal(list: unknown): string {
7 const r = parseSchedules(list);
8 if (r.ok) throw new Error("expected a refusal");
9 return r.error;
10 }
11
12 test("a bad cron string is refused naming the entry and the field", () => {
13 expect(refusal([good, { ...good, name: "nightly", cron: "99 * * * *" }])).toStartWith(
14 'forge.json: schedules[1] ("nightly").cron',
15 );
16 });
17
18 test("a missing path is refused naming the entry and the field", () => {
19 expect(refusal([{ name: "print", cron: "* * * * *" }])).toStartWith('forge.json: schedules[0] ("print").path');
20 });
21
22 test("a duplicate name is refused naming the second entry", () => {
23 expect(refusal([good, good])).toBe('forge.json: schedules[1] ("print").name "print" is used twice');
24 });
25
26 test("a bad name is refused by index without echoing it", () => {
27 expect(refusal([{ ...good, name: "Bad Name" }])).toBe(
28 "forge.json: schedules[0].name must be 1-40 lowercase letters, digits, - or _",
29 );
30 });
31
32 test("valid schedules still parse unchanged", () => {
33 const r = parseSchedules([good]);
34 expect(r.ok && r.value[0]?.timezone).toBe("UTC");
35 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs