json-logic-js icon indicating copy to clipboard operation
json-logic-js copied to clipboard

add typeof support

Open jottinger opened this issue 7 years ago • 3 comments

Some rules may want to include validation of data types. Imagine:

"typeof": [ "a", "string" ] -- evaluates to true if "a" is a string or not.

jottinger avatar Oct 22 '16 14:10 jottinger

The local tests.json file is downloaded from a remote site, so I can't commit the tests directly, but here's what they look like in my local branch, in the "single operator tests":

    [ {"typeof":["a","string"]}, {}, true ],
    [ {"typeof":[1,"number"]}, {}, true],
    [ {"typeof":[1,"string"]}, {}, false],
    [ {"typeof":[[1,2],"object"]}, {}, true],
    [ {"typeof":[1,"object"]}, {}, false],

jottinger avatar Oct 22 '16 14:10 jottinger

BTW, you can use your existing implementation as a custom operation starting in v1.0.9, like:

jsonLogic.add_operation("typeof", function(variable, type){ return typeof variable === type; });
jsonLogic.apply({"typeof":["a", "string"]});
jsonLogic.apply({"typeof":[1, "number"]});

Documentation is here: http://jsonlogic.com/add_operation.html

jwadhams avatar Oct 28 '16 21:10 jwadhams

Oh, you know what - I completely forgot about this. The day I got all of this, I had to go to the hospital, ended up having surgery and it completely flew off my radar.

On Fri, Oct 28, 2016 at 5:34 PM, Jeremy Wadhams [email protected] wrote:

BTW, you can use your existing implementation as a custom operation starting in v1.0.9, like:

jsonLogic.add_operation("typeof", function(variable, type){ return typeof variable === type; });jsonLogic.apply({"typeof":["a", "string"]});jsonLogic.apply({"typeof":[1, "number"]});

Documentation is here: http://jsonlogic.com/add_operation.html

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/jwadhams/json-logic-js/issues/8#issuecomment-257034535, or mute the thread https://github.com/notifications/unsubscribe-auth/AAcmmjb3WwYQRi4gOovIuD9FrESfwEXfks5q4mp8gaJpZM4Kd3dB .

jottinger avatar Nov 09 '16 19:11 jottinger