Repository: ChristopheBougere/asl-validator (ts)

Issue:
loc.indexOf is not a function (jsonpath-plus regression)
**Describe the bug**
2.2.0 with jsonpath-plus introduced a regression where an `OutputPath` like `$[(@.length-1)].bar` throws an error.

Same state machine definition passes validation in <2.2.0 and works in Step Functions.

**To Reproduce**
```json
{
  "StartAt": "map",
  "States": {
    "map": {
      "Type": "Map",
      "End": true,
      "InputPath": "$",
      "ItemsPath": "$.foo",
      "Parameters": {
        "bar.$": "$"
      },
      "Iterator": {
        "StartAt": "pass",
        "States": {
          "pass": {
            "Type": "Pass",
            "End": true
          }
        }
      },
      "ResultPath": "$",
      "OutputPath": "$[(@.length-1)].bar"
    }
  }
}
```

**Expected behavior**
This is a valid state machine, it should not error out with `loc.indexOf is not a function`.

**Version:** 2.2.0


Pull request:
feat: work around for JSONPath regression
the update to jsonpath-plus library changed to evaluate the expressions against the definition in lieu of having an exposed compile function

maybe I'm missing where parse/compile is exposed in JSONPath

for now, if the first call fails, we'll try it again w/ a non-empty array

closes #90 

Patch:
diff --git a/package-lock.json b/package-lock.json
index c6c7093..0ec6d6e 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1652,7 +1652,7 @@
     "concat-map": {
       "version": "0.0.1",
       "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz",
-      "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=",
+      "integrity": "sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==",
       "dev": true
     },
     "confusing-browser-globals": {
@@ -1989,7 +1989,7 @@
     "escape-string-regexp": {
       "version": "1.0.5",
       "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz",
-      "integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=",
+      "integrity": "sha512-vbRorB5FUQWvla16U8R/qgaFIya2qGzwDrNmCZuYKrbdSUMG6I1ZCGQRefkRVhuOkIGVne7BQ35DSfo1qvJqFg==",
       "dev": true
     },
     "escodegen": {
@@ -2628,7 +2628,7 @@
     "fast-levenshtein": {
       "version": "2.0.6",
       "resolved": "https://registry.npmjs.org/fast-levenshtein/-/fast-levenshtein-2.0.6.tgz",
-      "integrity": "sha1-PYpcZog6FqMMqGQ+hR8Zuqd5eRc=",
+      "integrity": "sha512-DCXu6Ifhqcks7TZKY3Hxp3y6qphY5SJZmrWMDrKcERSOXWQdMhU9Ig/PYrzyw/ul9jOIyh0N4M0tbC5hodg8dw==",
       "dev": true
     },
     "fastq": {
@@ -5665,7 +5665,7 @@
     "sprintf-js": {
       "version": "1.0.3",
       "resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz",
-      "integrity": "sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw=",
+      "integrity": "sha512-D9cPgkvLlV3t3IzL0D0YLvGA9Ahk4PcvVwUbN0dSGr1aP0Nrt4AEnTUbuGvquEC0mA64Gqt1fzirlRs5ibXx8g==",
       "dev": true
     },
     "sshpk": {
@@ -5791,7 +5791,7 @@
     "strip-bom": {
       "version": "3.0.0",
       "resolved": "https://registry.npmjs.org/strip-bom/-/strip-bom-3.0.0.tgz",
-      "integrity": "sha512-vavAMRXOgBVNF6nyEEmL3DBK19iRpDcoIwW+swQ+CbGiu7lju6t+JklA1MHweoWtadgt4ISVUsXLyDq34ddcwA==",
+      "integrity": "sha1-IzTBjpx1n3vdVv3vfprj1YjmjtM=",
       "dev": true
     },
     "strip-eof": {
diff --git a/src/lib/json-path-errors.js b/src/lib/json-path-errors.js
index 7dcfbfb..76b41e7 100644
--- a/src/lib/json-path-errors.js
+++ b/src/lib/json-path-errors.js
@@ -15,6 +15,12 @@ module.exports = (definition) => JSONPath({ json: definition, path: '$..[InputPa
       JSONPath({ path, json: definition });
       return null;
     } catch (e) {
+      try {
+        JSONPath({ path, json: [{}] });
+        return null;
+      } catch (e2) {
+        // ignore, return first error
+      }
       return e;
     }
   })


Test patch:
diff --git a/src/__tests__/definitions/valid-path-array-context.json b/src/__tests__/definitions/valid-path-array-context.json
new file mode 100644
index 0000000..838a804
--- /dev/null
+++ b/src/__tests__/definitions/valid-path-array-context.json
@@ -0,0 +1,11 @@
+{
+  "Comment": "Expression contains an array operator",
+  "StartAt": "Pass",
+  "States": {
+    "Pass": {
+      "Type": "Pass",
+      "OutputPath": "$[(@.length-1)].bar",
+      "End": true
+    }
+  }
+}