dbc

Design by contract and type checking assertions

npm install dbc
1 downloads in the last day
2 downloads in the last week
42 downloads in the last month

dbc is a small library for design-by-contract defensive coding in javascript.

It focuses especially on type assertions in an attempt to provide a small compensation for JavaScript's unfortunate dynamicness. Some of the ideas were borrowed from ristretto-js.

The core features are:

  • validate values against a specification
  • generate type constructors from a specification
  • runtime validation of function arguments and return value (via the wrap combinator)

API Documentation

Install

npm install dbc

Features

Check a value:

    dbc.type(1, 'number', 'optional error message');
    dbc.isFunction(function () {}, 'optional error message');
    dbc.functionArity(function () {}, 0, 'optional error message');
    // etc

Check an object:

    dbc.check({
        a: 1,
        b: "cat in the hat",
        c: function (f, s) { return f + s; }
    }, {
        a: [
            {validator: 'required', args: ['optional error message']}, 
            {validator: 'type', args: ['number']}, 
            {validator: 'custom', args: [ function (v) { 
                // custom validator function should return a boolean
                return v > 0; 
            }]}
        ],
        b: [{validator: 'type', args: ['string']}],
        c: [{validator: 'isFunction'}, {validator: 'functionArity', args: [2, {message: 'This is a more advanced error object', field: 'c'}]}]
    });

Validate an object (same as check except that it returns an array of errors instead of throwing an exception at the first error):

    dbc.validate({
        a: 1,
        b: "cat in the hat",
        c: function (f, s) { return f + s; }
    }, {
        a: [
            {validator: 'required', args: ['optional error message']}, 
            {validator: 'type', args: ['number']}, 
            {validator: 'custom', args: [ function (v) { 
                // custom validator function should return a boolean
                return v > 0; 
            }]}
        ],
        b: [{validator: 'type', args: ['string']}],
        c: [{validator: 'isFunction'}, {validator: 'functionArity', args: [2, {message: 'This is a more advanced error object', field: 'c'}]}]
    });

Test

npm install -g jasmine-node
jasmine-node --coffee spec/
npm loves you