Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/object-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,8 @@ __Examples__
__Signature:__`$error(message)`

Deliberately throws an error with an optional `message`

## `$assert()`
__Signature:__`$assert(condition, message)`

If condition is true, the function returns undefined. If the condition is false, an exception is thrown with the message as the message of the exception.
21 changes: 20 additions & 1 deletion src/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1822,6 +1822,25 @@ const functions = (() => {
};
}

/**
*
* @param {boolean} condition - the condition to evaluate
* @param {string} [message] - the message to attach to the error
* @throws custom error with code 'D3137'
* @returns {undefined}
*/
function assert(condition, message) {
if(!condition) {
throw {
code: "D3141",
stack: (new Error()).stack,
message: message || "$assert() statement failed"
};
}

return undefined;
}

/**
* Implements the merge sort (stable) with optional comparator function
*
Expand Down Expand Up @@ -1995,7 +2014,7 @@ const functions = (() => {
formatNumber, formatBase, number, floor, ceil, round, abs, sqrt, power, random,
boolean, not,
map, zip, filter, single, foldLeft, sift,
keys, lookup, append, exists, spread, merge, reverse, each, error, sort, shuffle, distinct,
keys, lookup, append, exists, spread, merge, reverse, each, error, assert, sort, shuffle, distinct,
base64encode, base64decode, encodeUrlComponent, encodeUrl, decodeUrlComponent, decodeUrl
};
})();
Expand Down
4 changes: 3 additions & 1 deletion src/jsonata.js
Original file line number Diff line number Diff line change
Expand Up @@ -1858,6 +1858,7 @@ var jsonata = (function() {
staticFrame.bind('reverse', defineFunction(fn.reverse, '<a:a>'));
staticFrame.bind('each', defineFunction(fn.each, '<o-f:a>'));
staticFrame.bind('error', defineFunction(fn.error, '<s?:x>'));
staticFrame.bind('assert', defineFunction(fn.assert, '<bs?:x>'));
staticFrame.bind('sort', defineFunction(fn.sort, '<af?:a>'));
staticFrame.bind('shuffle', defineFunction(fn.shuffle, '<a:a>'));
staticFrame.bind('distinct', defineFunction(fn.distinct, '<x:x>'));
Expand Down Expand Up @@ -1980,7 +1981,8 @@ var jsonata = (function() {
"D3137": "{{{message}}}",
"D3138": "The $single() function expected exactly 1 matching result. Instead it matched more.",
"D3139": "The $single() function expected exactly 1 matching result. Instead it matched 0.",
"D3140": "Malformed URL passed to ${{{functionName}}}(): {{value}}"
"D3140": "Malformed URL passed to ${{{functionName}}}(): {{value}}",
"D3141": "{{{message}}}"
};

/**
Expand Down
2 changes: 1 addition & 1 deletion test/test-suite/datasets/dataset5.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,4 @@
}
]
}
}
}
9 changes: 9 additions & 0 deletions test/test-suite/groups/function-assert/case000.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"expr": "$assert(Account.Order[0].Product[0].Price < 34, 'Too Expensive')",
"dataset": "dataset5",
"bindings": {},
"error": {
"code": "D3141",
"message": "Too Expensive"
}
}
6 changes: 6 additions & 0 deletions test/test-suite/groups/function-assert/case001.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"expr": "$assert(Account.Order[0].Product[0].Price < 35, 'Too Expensive')",
"dataset": "dataset5",
"bindings": {},
"undefinedResult": true
}
6 changes: 6 additions & 0 deletions test/test-suite/groups/function-assert/case002.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"expr": "($assert(Account.Order[0].Product[0].Price < 35, 'Too Expensive'); Account.Order[0].Product[0].Price)",
"dataset": "dataset5",
"bindings": {},
"result": 34.45
}
9 changes: 9 additions & 0 deletions test/test-suite/groups/function-assert/case003.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"expr": "($assert(Account.Order[0].Product[0].Price < 34, 'Too Expensive'); Account.Order[0].Product[0].Price)",
"dataset": "dataset5",
"bindings": {},
"error": {
"code": "D3141",
"message": "Too Expensive"
}
}
6 changes: 6 additions & 0 deletions test/test-suite/groups/function-assert/case004.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"expr": "$assert(null)",
"dataset": "dataset5",
"bindings": {},
"code": "T0410"
}
6 changes: 6 additions & 0 deletions test/test-suite/groups/function-assert/case005.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"expr": "$assert(5)",
"dataset": "dataset5",
"bindings": {},
"code": "T0410"
}
9 changes: 9 additions & 0 deletions test/test-suite/groups/function-assert/case006.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"expr": "$assert(false)",
"dataset": "dataset5",
"bindings": {},
"error": {
"code": "D3141",
"message": "$assert() statement failed"
}
}
6 changes: 6 additions & 0 deletions test/test-suite/groups/function-assert/case007.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"expr": "$assert(true)",
"dataset": "dataset5",
"bindings": {},
"undefinedResult": true
}