Repository: ChristopheBougere/asl-validator
Pull request #148 (2023-11-02 23:40:21)

fix: support path fields on fail for cause and error
This is a partial fix for the issue. I added support for the two new fields but typed them as a `string` since the existing validators for the ASL paths don't support the custom logic needed to accept a Reference Path and subset of Intrinsic Functions

This at least unblocks the use of the fields. 


Patch:
diff --git a/src/schemas/fail.json b/src/schemas/fail.json
index c654858..f0cd9c5 100644
--- a/src/schemas/fail.json
+++ b/src/schemas/fail.json
@@ -13,8 +13,16 @@
     "Cause": {
       "type": "string"
     },
+    "CausePath": {
+      "$comment": "This field is a ReferencePath with limited Intrinsic Function support. Using string until custom validation is available.",
+      "type": "string"
+    },
     "Error": {
       "type": "string"
+    },
+    "ErrorPath": {
+      "$comment": "This field is a ReferencePath with limited Intrinsic Function support. Using string until custom validation is available.",
+      "type": "string"
     }
   },
   "required": ["Type"],
diff --git a/src/types.ts b/src/types.ts
index 3b2dda2..0968dbe 100644
--- a/src/types.ts
+++ b/src/types.ts
@@ -22,6 +22,8 @@ export enum StateMachineErrorCode {
   MapToleratedFailureError = "MAP_TOLERATED_FAILURE",
   MapMaxConcurrencyError = "MAP_CONCURRENCY_ERROR",
   MapItemReaderMaxItemsError = "MAP_ITEMREADER_MAXITEM",
+  FailCauseProperty = "FAIL_CAUSE_PROPERTY",
+  FailErrorProperty = "FAIL_ERROR_PROPERTY",
 }
 export type StateMachineError = {
   "Error code": StateMachineErrorCode;
diff --git a/src/validator.ts b/src/validator.ts
index ec222d3..7f35682 100644
--- a/src/validator.ts
+++ b/src/validator.ts
@@ -148,6 +148,20 @@ export = function validator(
             errorCode: StateMachineErrorCode.MapItemBatcherError,
           }),
         },
+        {
+          filter: IsFail,
+          checker: AtMostOne({
+            props: ["Cause", "CausePath"],
+            errorCode: StateMachineErrorCode.FailCauseProperty,
+          }),
+        },
+        {
+          filter: IsFail,
+          checker: AtMostOne({
+            props: ["Error", "ErrorPath"],
+            errorCode: StateMachineErrorCode.FailErrorProperty,
+          }),
+        },
       ])
     );
   }


Test patch:
diff --git a/src/__tests__/definitions/invalid-fail-dupe-cause.json b/src/__tests__/definitions/invalid-fail-dupe-cause.json
new file mode 100644
index 0000000..b1a3e9c
--- /dev/null
+++ b/src/__tests__/definitions/invalid-fail-dupe-cause.json
@@ -0,0 +1,12 @@
+{
+  "Comment": "Contains both Cause and CausePath when only one is allowed",
+  "StartAt": "Hello",
+  "States": {
+    "Hello": {
+      "Type": "Fail",
+      "Cause": "CauseExample",
+      "CausePath": "$.cause",
+      "Error": "ErrorExample"
+    }
+  }
+}
diff --git a/src/__tests__/definitions/invalid-fail-dupe-error.json b/src/__tests__/definitions/invalid-fail-dupe-error.json
new file mode 100644
index 0000000..80d9062
--- /dev/null
+++ b/src/__tests__/definitions/invalid-fail-dupe-error.json
@@ -0,0 +1,11 @@
+{
+  "Comment": "Contains both Error and ErrorPath when only one is allowed",
+  "StartAt": "Hello",
+  "States": {
+    "Hello": {
+      "Type": "Fail",
+      "Error": "CauseExample",
+      "ErrorPath": "$.error"
+    }
+  }
+}
diff --git a/src/__tests__/definitions/valid-fail-paths.json b/src/__tests__/definitions/valid-fail-paths.json
new file mode 100644
index 0000000..9aad987
--- /dev/null
+++ b/src/__tests__/definitions/valid-fail-paths.json
@@ -0,0 +1,11 @@
+{
+  "Comment": "Contrived example with a Fail state to show it satisfies the terminal state requirement",
+  "StartAt": "Hello",
+  "States": {
+    "Hello": {
+      "Type": "Fail",
+      "CausePath": "$.path",
+      "ErrorPath": "States.Format('{}{}', $.field1, $field2)"
+    }
+  }
+}
diff --git a/src/__tests__/definitions/valid-fail.json b/src/__tests__/definitions/valid-fail.json
index 47156c3..7618494 100644
--- a/src/__tests__/definitions/valid-fail.json
+++ b/src/__tests__/definitions/valid-fail.json
@@ -3,7 +3,9 @@
   "StartAt": "Hello",
   "States": {
     "Hello": {
-      "Type": "Fail"
+      "Type": "Fail",
+      "Cause": "CauseExample",
+      "Error": "ErrorExample"
     }
   }
 }