Design by contract and type checking assertions


Keywords
dbc, validation, types
License
BSD-3-Clause
Install
npm install dbc@3.0.0

Documentation

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'}]}]
    });
Define a validated type of object:
    // create a constructor that automatically validates its schema
    var AjaxOptions = dbc.makeConstructor({
        type:       [
                        {validator:'type', args:['string']},
                        {validator:'oneOf', args:[['GET','POST','PUT','DELETE']]}
                    ],
        url:        [{validator:'type',args:['string']}],
        dataType:   [{validator:'type',args:['string?']}]
    });

    // define a function that validates its argument (is a string) and its return type (is an AjaxOptions)
    AjaxOptions.fromCorsRequest = dbc.wrap(function (corsRequestText) {
        return new AjaxOptions(new CorsRequest(corsRequestText).getJSON());
    }, {
        0: [{validator:'type', args:['string']}]
    }, [{validator:'isInstance',args:[AjaxOptions]}]);

Test

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