1.0.7 • Published 9 years ago

aproba-browser v1.0.7

Weekly downloads
2
License
ISC
Repository
github
Last release
9 years ago

aproba

This is the same as the aproba module except modified to not use Error.captureStackTrace which doesn't work in browsers. Admittedly, the trace gets some extraneous stuff, but I don't know a better way.


A ridiculously light-weight function argument validator

var validate = require("aproba-browser")

function myfunc(a, b, c) {
  // `a` must be a string, `b` a number, `c` a function
  validate('SNF', arguments) // [a,b,c] is also valid
}

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:

typedescription
*matches any type
AArray.isArray OR an arguments object
Stypeof == string
Ntypeof == number
Ftypeof == function
Otypeof == object and not type A and not type E
Btypeof == boolean
Einstanceof Error OR null

Validation failures throw one of three exception types, distinguished by a code property of EMISSINGARG, EINVALIDTYPE or ETOOMANYARGS.

If you pass in an invalid type then it will throw with a code of EUNKNOWNTYPE.

If an error argument is found and is not null then the remaining arguments will not be validated.

Why this exists

I wanted a very simple argument validator. It needed to do two things:

  1. Be more concise and easier to use than assertions

  2. Not encourage an infinite bikeshed of DSLs

This is why types are specified by a single character and there's no such thing as an optional argument.

This is not intended to validate user data. This is specifically about asserting the interface of your functions.

If you need greater validation, I encourage you to write them by hand or look elsewhere.

1.0.7

9 years ago

1.0.6

9 years ago

1.0.5

9 years ago

1.0.4

9 years ago