Skip to content
Lightweight validator for function arguments
Find file
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
.gitignore initial commit Dec 16, 2013
.travis.yml removed older node versions from travis config Dec 12, 2014 added propErr mode Jul 22, 2015


Lightweight validator for function arguments

Build Status


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


$ 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.


err(argsToTest, schema, [optionalSchema])

Returns null if there's no validation errors, otherwise it returns a text description separated by a comma.


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);

  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 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)");


MIT (see

Something went wrong with that request. Please try again.