Skip to content

Commit dcb52bc

Browse files
jhorbulykandrew-coleman
authored andcommitted
Add $assert() function. (#375)
* Add $assert() function. * Apply boolean check in the correct way.
1 parent 3cb51df commit dcb52bc

File tree

12 files changed

+86
-3
lines changed

12 files changed

+86
-3
lines changed

docs/object-functions.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,8 @@ __Examples__
5757
__Signature:__`$error(message)`
5858

5959
Deliberately throws an error with an optional `message`
60+
61+
## `$assert()`
62+
__Signature:__`$assert(condition, message)`
63+
64+
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.

src/functions.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1822,6 +1822,25 @@ const functions = (() => {
18221822
};
18231823
}
18241824

1825+
/**
1826+
*
1827+
* @param {boolean} condition - the condition to evaluate
1828+
* @param {string} [message] - the message to attach to the error
1829+
* @throws custom error with code 'D3137'
1830+
* @returns {undefined}
1831+
*/
1832+
function assert(condition, message) {
1833+
if(!condition) {
1834+
throw {
1835+
code: "D3141",
1836+
stack: (new Error()).stack,
1837+
message: message || "$assert() statement failed"
1838+
};
1839+
}
1840+
1841+
return undefined;
1842+
}
1843+
18251844
/**
18261845
* Implements the merge sort (stable) with optional comparator function
18271846
*
@@ -1995,7 +2014,7 @@ const functions = (() => {
19952014
formatNumber, formatBase, number, floor, ceil, round, abs, sqrt, power, random,
19962015
boolean, not,
19972016
map, zip, filter, single, foldLeft, sift,
1998-
keys, lookup, append, exists, spread, merge, reverse, each, error, sort, shuffle, distinct,
2017+
keys, lookup, append, exists, spread, merge, reverse, each, error, assert, sort, shuffle, distinct,
19992018
base64encode, base64decode, encodeUrlComponent, encodeUrl, decodeUrlComponent, decodeUrl
20002019
};
20012020
})();

src/jsonata.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1858,6 +1858,7 @@ var jsonata = (function() {
18581858
staticFrame.bind('reverse', defineFunction(fn.reverse, '<a:a>'));
18591859
staticFrame.bind('each', defineFunction(fn.each, '<o-f:a>'));
18601860
staticFrame.bind('error', defineFunction(fn.error, '<s?:x>'));
1861+
staticFrame.bind('assert', defineFunction(fn.assert, '<bs?:x>'));
18611862
staticFrame.bind('sort', defineFunction(fn.sort, '<af?:a>'));
18621863
staticFrame.bind('shuffle', defineFunction(fn.shuffle, '<a:a>'));
18631864
staticFrame.bind('distinct', defineFunction(fn.distinct, '<x:x>'));
@@ -1980,7 +1981,8 @@ var jsonata = (function() {
19801981
"D3137": "{{{message}}}",
19811982
"D3138": "The $single() function expected exactly 1 matching result. Instead it matched more.",
19821983
"D3139": "The $single() function expected exactly 1 matching result. Instead it matched 0.",
1983-
"D3140": "Malformed URL passed to ${{{functionName}}}(): {{value}}"
1984+
"D3140": "Malformed URL passed to ${{{functionName}}}(): {{value}}",
1985+
"D3141": "{{{message}}}"
19841986
};
19851987

19861988
/**

test/test-suite/datasets/dataset5.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,4 @@
7070
}
7171
]
7272
}
73-
}
73+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"expr": "$assert(Account.Order[0].Product[0].Price < 34, 'Too Expensive')",
3+
"dataset": "dataset5",
4+
"bindings": {},
5+
"error": {
6+
"code": "D3141",
7+
"message": "Too Expensive"
8+
}
9+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"expr": "$assert(Account.Order[0].Product[0].Price < 35, 'Too Expensive')",
3+
"dataset": "dataset5",
4+
"bindings": {},
5+
"undefinedResult": true
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"expr": "($assert(Account.Order[0].Product[0].Price < 35, 'Too Expensive'); Account.Order[0].Product[0].Price)",
3+
"dataset": "dataset5",
4+
"bindings": {},
5+
"result": 34.45
6+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"expr": "($assert(Account.Order[0].Product[0].Price < 34, 'Too Expensive'); Account.Order[0].Product[0].Price)",
3+
"dataset": "dataset5",
4+
"bindings": {},
5+
"error": {
6+
"code": "D3141",
7+
"message": "Too Expensive"
8+
}
9+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"expr": "$assert(null)",
3+
"dataset": "dataset5",
4+
"bindings": {},
5+
"code": "T0410"
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"expr": "$assert(5)",
3+
"dataset": "dataset5",
4+
"bindings": {},
5+
"code": "T0410"
6+
}

0 commit comments

Comments
 (0)