assert-args
Validate and format function arguments ( handles types and optionals).
Installation
npm install --save assert-args
Examples
assertArgs(arguments, validator)
Assert argument types and format arguments into an object
{ var args = foo = argsfoo bar = argsbar baz = argsbaz qux = argsqux corge = argscorge // will be an array /// ...}
Check out the examples below.
Type validations
- Supports '*', 'string', 'number', 'object', 'function', and classes (Array, RegExp, etc)
// Type examplevar args = /*Will throw an error if it does not pass: TypeError: "foo" must be a string*/// Multi-type examplevar args = /*Will throw an error if it does not pass: TypeError: "foo" must be a string or number*/ // Class examplevar args = /*Will throw an error if it does not pass: TypeError: "foo" must be an instance of Array*/
Custom validations
var args = // custom validation should throw an error if the value is invalid { if !valuebar throw '"bar" is required' }/*If `foo` does not pass it will throw: Error: "foo" is invalid: "bar" is required*/
Required arguments
var args =
Optional arguments
var args =
Spread arguments
var args =
Full examples
var args = /*args = { foo: 'str', bar: {}, qux: function () {}}*/ var args = /*args = { foo: 'str', bar: undefined, qux: function () {}}*/ var args = /*TypeError: "foo" is required*/ var args = /*TypeError: "qux" is required*/ var args = /*TypeError: "foo" must be a string*/ var args = /*TypeError: "qux" must be a function*/ var args = /*TypeError: "qux" is required*/ var args = /*args = { foo: ['one', 'two', 'three'], bar: function () {}}*/ var args = /*TypeError: "...foo" is required*/ var args = /*TypeError: "...foo" must be strings*/ // Check out the tests for more examples...
License
MIT