Have your arguments, and validate it too. -- Slick arguments validator for all your js functions.
Have your arguments, and validate it too:
var have = require'have';haveargumentsid : 'string or number'options : 'optional object'callback : 'function';
HAVE.js gives you a mini-DSL to quickly validate your function arguments.
In order of precedence:
opt X|optional X- Optional X
X or Y- Either X or Y
X a|X arr|x array- Array of X
These matchers can be combined. These are all valid HAVE.js matchers:
str or num array- String or Array of Number
num arr or str arr- Array of Number or Array of String
num a a a a- Array of Array of Array of Array of Number
opt str or num array- Optional (String or Array of Number)
The HAVE.js function also returns any parsed argument collected in a hash keyed to the same key as was given in the schema. You can inspect the returned object to more easily obtain the parsed value without having to duplicate the HAVE.js parsing logic in your code to extract them.
See @wmakeev PR for an example.
If you are like me and you write a lot of method preconditions that should be turned off or atleast, should not throws in production, you can replace HAVE.js assert function like so:
var have = require'have';have.assertif !condconsole.log'WARN: assertion failed: ' + message;;
This will replace the
assert function HAVE.js uses internally with your implementation
so if you want to completely turns assertion off, then just give it a no-op function.
For those who like it short, the above example can also be written like this:
var have = require'have';havearguments id: 's or n' options: 'opt o' callback: 'f' ;
This is not very readable, of course. But HAVE.js does not dictate your readability preference for you. So go wild if you think it is ok : )
BSD (if you don't like BSD, just contact me)
- (credit: @wmakeev) The function now returns parsed arguments as a hash object.
- Adds the forgotten
undefinedwhere optional argument is expected.
npm test or
Pull requests and feature suggestions totally welcome.