It looks like a bug in JugglingDB (the ORM used by CompoundJS).
Consider the following code:
var juggling = require('jugglingdb');
var Schema = juggling.Schema;
var schema = new Schema('memory');
var User = schema.define('User');
var num = /^\s*\d+\s*$/;
User.validatesPresenceOf('id', {message: "can't be blank"});
User.validatesFormatOf('id', {with: num, message:"is not a number"});
User.validatesFormatOf('id', {with: num, message:"is not a number"});
var user = new User({ id : '' });
user.isValid(function(valid) {
if (! valid)
{
console.log('invalid', user.errors);
}
else
{
console.log('valid');
}
});
This will actually generate 3 errors:
invalid { id: [ 'can\'t be blank', 'is blank', 'is blank' ] }
If you add another User.validatesFormat
, it will generate 4 errors. It looks like a scoping problem somewhere in the validation code for JugglingDB, I'll post a bug report.
For now, perhaps you could just use the first error that occurs (user.errors[0]
). EDIT: by the looks of it, that's what you also have to do if it would have worked, since JugglingDB will run all validation tests and generate an error for all failing tests.