0.1.0 • Published 9 years ago

arg-err v0.1.0

Weekly downloads
5
License
MIT
Repository
github
Last release
9 years ago

ARG-ERR

Lightweight validator for function arguments

Build Status

Features

arg-err supports:

  • validating against a type name (e.g. string or number)
  • 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 = require("arg-err");

function frobnicate(args, callback) {
  var err = arg.err(args, {
    foo: "number",
    bar: "string",
    baz: "regexp"
  });

  if (err) {
    return callback(err);
  }
}

frobnicate({
  foo: 123,
  bar: 456
}, function (err) {
  assert.equal(err, "expected argument bar to be of type string (was number), expected argument baz to be of type regexp");
});

Nested schemas and regexes

var args = { foo: { bar: 123 }, baz: "bla" },
  err = arg.err(args, {
    foo: { bar: "string" },
    baz: /^qux$/
  });

assert.equal(err, "expected argument foo.bar to be of type string (was number), expected argument baz to match /^qux$/ (was \"bla\")");

Multiple possible arguments

var args = { foo: /reg[exp]$/ },
  err = arg.err(args, {
    foo: ["string", "number"]
  });

assert.equal(err, "expected argument foo to be of type string or number (was regexp)");

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.

function isEven(foo) {
  return foo % 2 === 0;
}

var args = { foo: 13 },
  err = arg.err(args, {
    foo: isEven
  });

assert.equal(err, "expected argument foo to pass isEven");

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.err(args, {
    foo: "number"
  }, {
    bar: "number"
  });

assert.equal(err, "expected optional argument bar to be of type number (was string)");

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 = require("arg-err").config({ propErr: true });

var err = prop.err({ foo: "2" }, { foo: "number" });

assert.equal(err, "expected property foo to be of type number (was string)");

License

MIT (see LICENSE.md).

0.1.0

9 years ago

0.0.9

10 years ago

0.0.8

10 years ago

0.0.7

10 years ago

0.0.6

10 years ago

0.0.5

10 years ago

0.0.4

10 years ago

0.0.3

10 years ago

0.0.2

10 years ago