Reference API documentation for robocop.js.
robocop
is the global variable exposed by robocop.js.
Return the list of data types registered with robocop.js.
// data types provided by robocop
robocop.availableDataTypes(); // [
// "string",
// "number",
// "integer",
// "float",
// "array",
// "object",
// "boolean",
// "date"
// ]
Return the list of rules registered with robocop.js.
// rules provided by robocop
robocop.availableRules(); // [
// "nullable",
// "max",
// "min",
// "maxLength",
// "minLength",
// "type"
// ]
Return the list of schemas registered with robocop.js.
// schemas provided by robocop
robocop.availableSchemas(); // []
// define your own, that's the point!
robocop.defineSchema('mySchema', {
name: {
type: 'string'
}
});
robocop.availableSchemas(); // ['mySchema']
Register a new data type with robocop.js.
Type: string
Required: Yes
Description: The name by which this data type will be identified.
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.
robocop.defineDataType('NaN', function (x) {
if (isNaN(x)) {
return null;
} else {
return {
rule: 'type',
actual: typeof x,
expected: 'NaN'
};
}
});
Register a new rule with robocop.js.
Type: string
Required: Yes
Description: The name by which this rule will be identified.
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.
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.
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'
// } ]
// }
// }
Register a new schema with robocop.js.
Type: string
Required: Yes
Description: The name by which this schema will be identified.
Type: object
Required: Yes
Description: Object that maps rules to properties.
robocop.defineSchema('person', {
name: {
given: {
type: 'string',
maxLength: 255
},
surname: {
type: 'string',
maxLength: 255
}
},
age: {
type: 'number',
max: 150,
min: 0
}
});
Retrieve the data type with the given name from robocop's registry.
Type: string
Required: Yes
Description: The name of the data type to retrieve.
var NaNDataType = robocop.getDataType('NaN');
var errors = NaNDataType(30);
errors; // {
// rule: 'type',
// actual: 'number',
// expected: 'NaN'
// }
errors = NaNDataType(NaN);
errors; // null
Retrieve the rule with the given name from robocop's registry.
Type: string
Required: Yes
Description: The name of the data type to retrieve.
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'
// }
Retrieve the schema with the given name from robocop's registry.
Type: string
Required: Yes
Description: The name of the schema to retrieve.
var PersonSchema = robocop.getSchema('person');
var errors = PersonSchema.validateSync({
name: {
given: 'John',
surname: 'Anderson
},
age: 30
});
errors; // null
Remove the data type with the given name from robocop's registry.
Type: string
Required: Yes
Description: The name of the data type to remove.
robocop.removeDataType('NaN');
robocop.getDataType('NaN') // undefined;
Remove the rule with the given name from robocop's registry.
Type: string
Required: Yes
Description: The name of the rule to remove.
robocop.removeRule('divisibleBy');
robocop.getRule('divisibleBy') // undefined;
Remove the schema with the given name from robocop's registry.
Type: string
Required: Yes
Description: The name of the schema to remove.
robocop.removeSchema('person');
robocop.getSchema('person') // undefined;
Schema class used internally by robocop
. You can use Schema
directly if you'd like.
// 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'
}
});
Validate the given attributes against this Schema instance.
Type: object
Required: Yes
Description: The hash of attributes to validate.
Type: object
Required: No
Description: Optional configuration.
Type: function
Required: Yes
Description: Callback function. Signature: cb(err)
. err
will be null if validation succeeds.
mySchema.validate({
name: 'John Anderson'
}, function (err) {
err; // null
});
mySchema.validate({
name: 5
}, function (err) {
err; // {
// name: {
// errors: [{
// rule: 'type',
// actual: 'number',
// expected: 'string'
// }]
// }
// }
});
Synchronously validate the given attributes against this Schema instance.
Type: object
Required: Yes
Description: The hash of attributes to validate.
Type: object
Required: No
Description: Optional configuration.
var errors = mySchema.validate({
name: 'John Anderson'
});
errors; // null
errors = mySchema.validate({
name: 5
});
errors; // {
// name: {
// errors: [{
// rule: 'type',
// actual: 'number',
// expected: 'string'
// }]
// }
// }
METHOD NOT IMPLEMENTED
Attempt to coerce the given attributes to their proper data types as specified by any "type" rules in this Schema.
Type: object
Required: Yes
Description: The hash of attributes to coerce.
METHOD NOT IMPLEMENTED
METHOD NOT IMPLEMENTED
Fill in default values for those attributes that are missing, as specified by any "default" rules in this Schema.
Type: object
Required: Yes
Description: The hash of attributes to which to fill in default values.
METHOD NOT IMPLEMENTED