npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

arg-err

v0.1.0

Published

Lightweight validator for function arguments

Downloads

20

Readme

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