This is an automated email from the ASF dual-hosted git repository.

jason810496 pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new 53d1c454e43 TS SDK: Reject an unknown trigger rule or weight rule in a 
task spec (#77392)
53d1c454e43 is described below

commit 53d1c454e4371e8b4fcae0f3eeec8d1d3005d542
Author: Adam Postek <[email protected]>
AuthorDate: Thu Nov 26 03:04:44 2026 +0000

    TS SDK: Reject an unknown trigger rule or weight rule in a task spec 
(#77392)
---
 ts-sdk/src/sdk/dag.ts        | 41 ++++++++++++++++++++++++++++++++++++
 ts-sdk/tests/sdk/dag.test.ts | 50 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 91 insertions(+)

diff --git a/ts-sdk/src/sdk/dag.ts b/ts-sdk/src/sdk/dag.ts
index 5689a27e195..6de329d33dc 100644
--- a/ts-sdk/src/sdk/dag.ts
+++ b/ts-sdk/src/sdk/dag.ts
@@ -1150,6 +1150,7 @@ export class Dag {
       spec[key] = value[key];
     }
     rejectBlankQueue(spec["queue"], `Dag "${this.dagId}" task "${taskId}"`);
+    rejectUnknownRules(spec, `Dag "${this.dagId}" task "${taskId}"`);
     return spec as TaskSpec;
   }
 
@@ -1581,6 +1582,46 @@ function rejectBlankQueue(queue: unknown, owner: 
string): void {
   }
 }
 
+// The Dag schema types trigger_rule and weight_rule as plain strings, so 
these mirror
+// airflow/task/trigger_rule.py and weight_rule.py; dag.test.ts checks they 
still match.
+export const TRIGGER_RULES: ReadonlySet<string> = new Set([
+  "all_success",
+  "all_failed",
+  "all_done",
+  "all_done_min_one_success",
+  "all_done_setup_success",
+  "one_success",
+  "one_failed",
+  "one_done",
+  "none_failed",
+  "none_skipped",
+  "always",
+  "none_failed_min_one_success",
+  "all_skipped",
+]);
+export const WEIGHT_RULES: ReadonlySet<string> = new Set(["downstream", 
"upstream", "absolute"]);
+
+// Airflow accepts an unknown rule when the Dag is saved: a trigger rule then 
leaves the task
+// never running and a weight rule fails every run of the Dag. A value that is 
not a string is
+// left to the serializer, which rejects it.
+function rejectUnknownRules(spec: Record<string, unknown>, owner: string): 
void {
+  const triggerRule = spec["triggerRule"];
+  if (typeof triggerRule === "string" && !TRIGGER_RULES.has(triggerRule)) {
+    throw new Error(
+      `triggerRule in the spec for ${owner} is ${JSON.stringify(triggerRule)}; 
` +
+        `use one of ${[...TRIGGER_RULES].join(", ")}`,
+    );
+  }
+  const weightRule = spec["weightRule"];
+  // Python also accepts a plugin's PriorityWeightStrategy import path; like 
the Go SDK, this does not.
+  if (typeof weightRule === "string" && !WEIGHT_RULES.has(weightRule)) {
+    throw new Error(
+      `weightRule in the spec for ${owner} is ${JSON.stringify(weightRule)}; ` 
+
+        `use one of ${[...WEIGHT_RULES].join(", ")}`,
+    );
+  }
+}
+
 /**
  * Internal: the task records of a Dag, for bundle lookups.
  *
diff --git a/ts-sdk/tests/sdk/dag.test.ts b/ts-sdk/tests/sdk/dag.test.ts
index b608f394426..81bba8afd6c 100644
--- a/ts-sdk/tests/sdk/dag.test.ts
+++ b/ts-sdk/tests/sdk/dag.test.ts
@@ -17,6 +17,9 @@
  * under the License.
  */
 
+import { existsSync, readFileSync } from "node:fs";
+import { dirname, join } from "node:path";
+import { fileURLToPath } from "node:url";
 import { describe, it, expect } from "vitest";
 import {
   Dag,
@@ -26,6 +29,8 @@ import {
   getDagTaskInputs,
   getDagTaskRecords,
   Label,
+  TRIGGER_RULES,
+  WEIGHT_RULES,
   type DagSpec,
   type TaskRef,
   type TaskSpec,
@@ -417,6 +422,51 @@ describe("Dag", () => {
     expect(dag.taskIds).toEqual([]);
   });
 
+  it.each(["all_sucess", "ALL_SUCCESS", ""])(
+    "rejects the unknown trigger rule %j",
+    (triggerRule) => {
+      const dag = new Dag("example_dag");
+      expect(() => dag.task("transform", async () => undefined, { triggerRule 
})).toThrowError(
+        `triggerRule in the spec for Dag "example_dag" task "transform" is 
${JSON.stringify(triggerRule)}; ` +
+          "use one of all_success,",
+      );
+      expect(dag.taskIds).toEqual([]);
+    },
+  );
+
+  it.each(["upwards", "Upstream", "", 
"my_plugin.strategies.DecreasingPriorityStrategy"])(
+    "rejects the unknown weight rule %j",
+    (weightRule) => {
+      const dag = new Dag("example_dag");
+      expect(() => dag.task("transform", async () => undefined, { weightRule 
})).toThrowError(
+        `weightRule in the spec for Dag "example_dag" task "transform" is 
${JSON.stringify(weightRule)}; ` +
+          "use one of downstream, upstream, absolute",
+      );
+      expect(dag.taskIds).toEqual([]);
+    },
+  );
+
+  it("accepts every trigger rule and weight rule", () => {
+    const dag = new Dag("example_dag");
+    for (const triggerRule of TRIGGER_RULES)
+      dag.task(`t_${triggerRule}`, async () => undefined, { triggerRule });
+    for (const weightRule of WEIGHT_RULES)
+      dag.task(`w_${weightRule}`, async () => undefined, { weightRule });
+    expect(dag.taskIds).toHaveLength(TRIGGER_RULES.size + WEIGHT_RULES.size);
+  });
+
+  const coreDir = join(dirname(fileURLToPath(import.meta.url)), 
"../../../airflow-core");
+
+  // Skipped outside the monorepo, such as in an extracted source release.
+  it.skipIf(!existsSync(coreDir)).each([
+    ["trigger_rule.py", TRIGGER_RULES],
+    ["weight_rule.py", WEIGHT_RULES],
+  ])("matches the values of Airflow's %s", (file, rules) => {
+    const source = readFileSync(join(coreDir, "src/airflow/task", file), 
"utf8");
+    const values = [...source.matchAll(/^ {4}[A-Z_]+ = 
"([a-z_]+)"$/gm)].map((match) => match[1]);
+    expect(new Set(values)).toEqual(rules);
+  });
+
   it.each([
     ["null", null],
     ["an array", []],

Reply via email to