json-logic-js
json-logic-js copied to clipboard
Add comments and trace failed rules when false
Hi,
New to Github... I'm using a fork of jsonLogic for a product configurator we're developing, which will need regular updates of the rule set, so it fits our needs.
I've added two small features that we found handy: 1) the ability to have comments in the JSON file to explain complex rules and 2) a (still rather coarse) way of reporting which rule(s) fail when the result is false. Perhaps this may be useful to others too?
Since JSON doesn't allow comments, I've basically just added the operators // and # which don't do anything. This allows me to add 'comments' like this:
{"//": [
"THIS IS A SAMPLE COMMENT",
"AND THE SECOND LINE OF THIS COMMENT. OR EVEN A THIRD...",
"OR A NEW COMMENT, WHATEVER YOU WISH!"
]}
Not a paragon of elegance, but it does the trick.
The trace method I added simply returns a list of the rule(s) that failed with expected value and the actual value.
Cheers, Elco
That's not so hard to do https://github.com/ivan133/json-logic-js/commit/4b96de92e795a119e200570a213bef0267e0a5e4