Merge pull request #292 from fabien/fix/refactor-validations

Fix multi-property validation definitions
This commit is contained in:
Raymond Feng 2014-09-05 09:16:55 -07:00
commit b364a57cf7
2 changed files with 14 additions and 7 deletions

View File

@ -641,13 +641,14 @@ function configure(cls, validation, args, opts) {
conf.customValidator = args.pop(); conf.customValidator = args.pop();
} }
conf.validation = validation; conf.validation = validation;
var attr = args[0]; args.forEach(function (attr) {
if (typeof attr === 'string') { if (typeof attr === 'string') {
var validation = extend({}, conf); var validation = extend({}, conf);
validation.options = opts || {}; validation.options = opts || {};
cls.validations[attr] = cls.validations[attr] || []; cls.validations[attr] = cls.validations[attr] || [];
cls.validations[attr].push(validation); cls.validations[attr].push(validation);
} }
});
} }
function Errors() { function Errors() {

View File

@ -145,9 +145,15 @@ describe('validations', function () {
it('should validate presence', function () { it('should validate presence', function () {
User.validatesPresenceOf('name', 'email'); User.validatesPresenceOf('name', 'email');
var validations = User.validations;
validations.name.should.eql([{validation: 'presence', options: {}}]);
validations.email.should.eql([{validation: 'presence', options: {}}]);
var u = new User; var u = new User;
u.isValid().should.not.be.true; u.isValid().should.not.be.true;
u.name = 1; u.name = 1;
u.isValid().should.not.be.true;
u.email = 2; u.email = 2;
u.isValid().should.be.true; u.isValid().should.be.true;
}); });