Aproba

aproba

A ridiculously light-weight function argument validator

aproba is of a kind an argument validator, that is concise and easier to use than assertions and doesn't encourage an infinite bikeshed of DSLs.

Get it: npm install aproba

Sample usage:

1
2
3
4
5
6
7
8
9
10
const validate = require("aproba")

const myfunc = (...args) {
  validate('SNF', args)
}

myfunc('test', 23, function () {}) // ok
myfunc(123, 23, function () {}) // type error
myfunc('test', 23) // missing arg error
myfunc('test', 23, function () {}, true) // too many args error

Valid types are:

type description
* matches any type
A Array.isArray OR an arguments object
S typeof == string
N typeof == number
F typeof == function
O typeof == object and not type A and not type E
B typeof == boolean
E instanceof Error OR null (special: see below)
Z == null

Validation failures throw one of the following code errors:

  • EMISSINGARG
  • EINVALIDTYPE
  • ETOOMANYARGS
  • EUNKNOWNTYPE (for an invalid type)

P.S: For optinal args one can use a | ex: SO|S.

GIF FTW!

aproba.gif

Suggest a module

Comments