Fork me on GitHub

robocop.js

Define and validate rules, datatypes and schemata in Node and in the browser.

What is robocop.js?

robocop.js is a library that allows you to define and validate rules, datatypes and schemata in Node and in the browser. Check it out:

Define a rule

robocop.defineRule('divisibleBy', function (x, divisor) {
    if (typeof x === 'number' && typeof divisor === 'number' && x % divisor !== 0) {
        return {
            rule: 'divisibleBy',
            actual: '' + x + ' % ' + divisor + ' === ' + (x % divisor),
            expected: '' + x + ' % ' + divisor + ' === 0'
        };
    }
    return null;
});

Define a schema

robocop.defineSchema('mySchema', {
    seats: {
        divisibleBy: 4
    }
});

var errors = robocop.getSchema('mySchema').validateSync({
    seats: 16
});

errors; //  null

errors = robocop.getSchema('mySchema').validateSync({
    seats: 17
});

errors; //  {
        //      seats: {
        //          errors: [ {
        //              rule: 'divisibleBy',
        //              actual: '17 % 4 === 1',
        //              expected: '17 % 4 === 0'
        //          } ]
        //      }
        //  }

Another example:

robocop.defineSchema('person', {
    name: {
        given: {
            type: 'string',
            maxLength: 255
        },
        surname: {
            type: 'string',
            maxLength: 255
        }
    },
    age: {
        type: 'number',
        max: 150,
        min: 0
    }
});

Validate attributes against a schema

var errors = robocop.getSchema('person').validateSync({
    name: {
        given: 'John',
        surname: 'Anderson'
    },
    age: 30
});

errors; // null

Get an error object when the test fails

errors = robocop.getSchema('person').validateSync({
    name: {
        given: 'John',
        surname: 'Anderson'
    },
    age: -4
});

errors; //  {
        //      age: {
        //          errors: [{
        //              rule: 'min',
        //              actual: '150 < 0',
        //              expected: '150 >= 0',
        //          }]
        //      }
        //  }

NPM

Master
Version NPM version
Source master
Build Status Build Status
Coverage Coverage Status
Code Climate Code Climate
Dependency Status Dependency Status

Installation

Installation - Read how to install robocop.js and get it running in your application.

Guide

Guide - Learn how to use robocop.js and read about common usage patterns.

API

API - Reference API documentation for robocop.js.

Community

Mailing List - Ask questions and get help.

Issues - Found a bug? Submit an issue!

GitHub - View the source code for robocop.js.