ARG-ERR
Lightweight validator for function arguments
Features
arg-err supports:
- validating against a type name (e.g.
string
ornumber
) - validating a string argument against a regex
- validating against multiple possible types defined as an array (e.g.
["string", "number"]
) - validating against a nested schema
- validating against a function for slightly more complex logic
- optional arguments
Installing
$ npm install arg-err
dist/arg-err.js
has been compiled with Browserify, feel free to drop that in your client-side project!
Supports IE9+ and all the other usual suspects.
API
err(argsToTest, schema, [optionalSchema])
Returns null
if there's no validation errors, otherwise it returns a text description separated by a comma.
Example
Basic usage
var arg = ; { var err = arg; if err return ; } ;
Nested schemas and regexes
var args = foo: bar: 123 baz: "bla" err = arg; assert;
Multiple possible arguments
var args = foo: /reg[exp]$/ err = arg; assert;
Complex validation
Sometimes you do need that extra boost.
arg-err
uses the method name in the validation message, so don't use anonymous functions if you want a sane message.
{ return foo % 2 === 0;} var args = foo: 13 err = arg; assert;
Optional arguments
Optional arguments are handled exactly the same as normal ones, except no error is thrown if the property is undefined.
var args = foo: 123 bar: "bla" err = arg; assert;
propErr mode
What you're doing is validating properties on an object. If you'd rather be a bit clearer about it, you can switch to propErr
mode.
var prop = ; var err = prop; assert;
License
MIT (see LICENSE.md).