Repository: jsonata-js/jsonata (2026-07-02T21:32:11Z)

Issue:
Bug: Accessing undefined property with a function results in an error
Consider the following JSONata expression:

```
(
  $foo := {};
  $foo.bar[$contains("123", $lowercase())]
)
```

Result: `undefined is not an object (evaluating 'e.apply')`

Note that if I replace `$contains("123", $lowercase())` with an actual result of this call (either `true` or `false`) it works properly and returns `undefined`.

Link to JSONata Exerciser: [https://try.jsonata.org/gnq5qjXA_](https://try.jsonata.org/gnq5qjXA_)

*** 

Edit:

Digging into this a bit more, we discovered that when the second arg of `$contains` resolves to `undefined` - it crashes: https://try.jsonata.org/2cN8l5GkQ

Edit 2:

Looks like, if the second arg is not a string - then JSONata thinks it’s a matcher function: https://github.com/jsonata-js/jsonata/blob/master/src/functions.js#L349-L354


Fix: https://github.com/Stedi/jsonata/pull/18


Discussion:

Patch:
diff --git a/src/functions.js b/src/functions.js
index 6f1d035a..1c0f8f56 100644
--- a/src/functions.js
+++ b/src/functions.js
@@ -342,7 +342,7 @@ const functions = (() => {
      */
     async function contains(str, token) {
         // undefined inputs always return undefined
-        if (typeof str === 'undefined') {
+        if (typeof str === 'undefined' || typeof token === 'undefined') {
             return undefined;
         }
 


Test patch:
diff --git a/test/test-suite/groups/function-contains/case007.json b/test/test-suite/groups/function-contains/case007.json
new file mode 100644
index 00000000..e35fac4c
--- /dev/null
+++ b/test/test-suite/groups/function-contains/case007.json
@@ -0,0 +1,6 @@
+{
+    "expr": "$contains(\"Hello World\", nothing)",
+    "dataset": null,
+    "bindings": {},
+    "undefinedResult": true
+}