Fork me on GitHub

API

Reference API documentation for robocop.js.

Description

robocop is the global variable exposed by robocop.js.

Description

Return the list of data types registered with robocop.js.

Usage

// data types provided by robocop
robocop.availableDataTypes();   //  [
                                //      "string",
                                //      "number",
                                //      "integer",
                                //      "float",
                                //      "array",
                                //      "object",
                                //      "boolean",
                                //      "date"
                                //  ]

Description

Return the list of rules registered with robocop.js.

Usage

// rules provided by robocop
robocop.availableRules();   //  [
                            //      "nullable",
                            //      "max",
                            //      "min",
                            //      "maxLength",
                            //      "minLength",
                            //      "type"
                            //  ]

Description

Return the list of schemas registered with robocop.js.

Usage

// schemas provided by robocop
robocop.availableSchemas();   // []

// define your own, that's the point!
robocop.defineSchema('mySchema', {
    name: {
        type: 'string'
    }
});

robocop.availableSchemas();   // ['mySchema']

Description

Register a new data type with robocop.js.

Parameters

name

Type: string

Required: Yes

Description: The name by which this data type will be identified.

typeDefinition

Type: function

Required: Yes

Description: Function that accepts the value to be tested. This function should return null if the value is considered valid, otherwise any return value will be used as the error message.

Usage

robocop.defineDataType('NaN', function (x) {
    if (isNaN(x)) {
        return null;
    } else {
        return {
            rule: 'type',
            actual: typeof x,
            expected: 'NaN'
        };
    }
});

Description

Register a new rule with robocop.js.

Parameters

name

Type: string

Required: Yes

Description: The name by which this rule will be identified.

ruleFunc

Type: function

Required: Yes

Description: Function that accepts the value to be tested. This function should return null if the value passes the rule's test, otherwise any return value will be used as the error message.

async

Type: boolean

Required: false

Description: Whether this is an asynchronous rule. If true, a third argument will be passed to this rule which will be a callback function to be executed when the rule is done. Any argument passed to the callback will be considered a validation failure.

Usage

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

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'
        //          } ]
        //      }
        //  }

Description

Register a new schema with robocop.js.

Parameters

name

Type: string

Required: Yes

Description: The name by which this schema will be identified.

definition

Type: object

Required: Yes

Description: Object that maps rules to properties.

Usage

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

Description

Retrieve the data type with the given name from robocop's registry.

Parameters

name

Type: string

Required: Yes

Description: The name of the data type to retrieve.

Usage

var NaNDataType = robocop.getDataType('NaN');

var errors = NaNDataType(30);

errors; //  {
        //      rule: 'type',
        //      actual: 'number',
        //      expected: 'NaN'
        //  }

errors = NaNDataType(NaN);

errors; // null

Description

Retrieve the rule with the given name from robocop's registry.

Parameters

name

Type: string

Required: Yes

Description: The name of the data type to retrieve.

Usage

var DivisibleByRule = robocop.getRule('divisibleBy');

var errors = DivisibleByRule(16, 4);

errors; //  null

errors = DivisibleByRule(17, 4);

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

Description

Retrieve the schema with the given name from robocop's registry.

Parameters

name

Type: string

Required: Yes

Description: The name of the schema to retrieve.

Usage

var PersonSchema = robocop.getSchema('person');

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

errors; // null

Description

Remove the data type with the given name from robocop's registry.

Parameters

name

Type: string

Required: Yes

Description: The name of the data type to remove.

Usage

robocop.removeDataType('NaN');

robocop.getDataType('NaN') // undefined;

Description

Remove the rule with the given name from robocop's registry.

Parameters

name

Type: string

Required: Yes

Description: The name of the rule to remove.

Usage

robocop.removeRule('divisibleBy');

robocop.getRule('divisibleBy') // undefined;

Description

Remove the schema with the given name from robocop's registry.

Parameters

name

Type: string

Required: Yes

Description: The name of the schema to remove.

Usage

robocop.removeSchema('person');

robocop.getSchema('person') // undefined;

Description

Schema class used internally by robocop. You can use Schema directly if you'd like.

Usage

// A standalone schema
var schema = new robocop.Schema('mySchema', {
    name: {
        type: 'string'
    }
});
// This schema will be saved in robocop's registry
var schema = robocop.defineSchema('mySchema', {
    name: {
        type: 'string'
    }
});

Description

Validate the given attributes against this Schema instance.

Parameters

attrs

Type: object

Required: Yes

Description: The hash of attributes to validate.

options

Type: object

Required: No

Description: Optional configuration.

cb

Type: function

Required: Yes

Description: Callback function. Signature: cb(err). err will be null if validation succeeds.

Usage

mySchema.validate({
    name: 'John Anderson'
}, function (err) {
    err; // null
});

mySchema.validate({
    name: 5
}, function (err) {
    err;    //  {
            //      name: {
            //          errors: [{
            //              rule: 'type',
            //              actual: 'number',
            //              expected: 'string'
            //          }]
            //      }
            //  }
});

Description

Synchronously validate the given attributes against this Schema instance.

Parameters

attrs

Type: object

Required: Yes

Description: The hash of attributes to validate.

options

Type: object

Required: No

Description: Optional configuration.

Usage

var errors = mySchema.validate({
    name: 'John Anderson'
});

errors; // null

errors = mySchema.validate({
    name: 5
});
errors; //  {
        //      name: {
        //          errors: [{
        //              rule: 'type',
        //              actual: 'number',
        //              expected: 'string'
        //          }]
        //      }
        //  }

Description

METHOD NOT IMPLEMENTED

Attempt to coerce the given attributes to their proper data types as specified by any "type" rules in this Schema.

Parameters

attrs

Type: object

Required: Yes

Description: The hash of attributes to coerce.

Usage

METHOD NOT IMPLEMENTED

Description

METHOD NOT IMPLEMENTED

Fill in default values for those attributes that are missing, as specified by any "default" rules in this Schema.

Parameters

attrs

Type: object

Required: Yes

Description: The hash of attributes to which to fill in default values.

Usage

METHOD NOT IMPLEMENTED