More validations and tests

Added validatesAbsenceOf. Handle async with if/unless prevention of
validators correctly. See: #170 #158
This commit is contained in:
Fabien Franzen 2014-07-11 22:56:02 +02:00
parent 5f1431aa05
commit a58dbe3a54
2 changed files with 90 additions and 5 deletions

View File

@ -46,6 +46,20 @@ function Validatable() {
*/ */
Validatable.validatesPresenceOf = getConfigurator('presence'); Validatable.validatesPresenceOf = getConfigurator('presence');
/**
* Validate absence of one or more specified properties.
* A model should not include a property to be considered valid; fails when validated field not blank.
*
* For example, validate absence of reserved
* ```
* Post.validatesAbsenceOf('reserved', { unless: 'special' });
*
* @param {String} propertyName One or more property names.
* @options {Object} errMsg Optional custom error message. Default is "can't be set"
* @property {String} message Error message to use instead of default.
*/
Validatable.validatesAbsenceOf = getConfigurator('absence');
/** /**
* Validate length. Require a property length to be within a specified range. * Validate length. Require a property length to be within a specified range.
* Three kinds of validations: min, max, is. * Three kinds of validations: min, max, is.
@ -225,6 +239,15 @@ function validatePresence(attr, conf, err) {
} }
} }
/*!
* Absence validator
*/
function validateAbsence(attr, conf, err) {
if (!blank(this[attr])) {
err();
}
}
/*! /*!
* Length validator * Length validator
*/ */
@ -332,6 +355,7 @@ function validateUniqueness(attr, conf, err, done) {
var validators = { var validators = {
presence: validatePresence, presence: validatePresence,
absence: validateAbsence,
length: validateLength, length: validateLength,
numericality: validateNumericality, numericality: validateNumericality,
inclusion: validateInclusion, inclusion: validateInclusion,
@ -470,8 +494,11 @@ function validationFailed(inst, v, cb) {
// here we should check skip validation conditions (if, unless) // here we should check skip validation conditions (if, unless)
// that can be specified in conf // that can be specified in conf
if (skipValidation(inst, conf, 'if')) return false; if (skipValidation(inst, conf, 'if')
if (skipValidation(inst, conf, 'unless')) return false; || skipValidation(inst, conf, 'unless')) {
if (cb) cb(true);
return false;
}
var fail = false; var fail = false;
var validator = validators[conf.validation]; var validator = validators[conf.validation];
@ -500,7 +527,7 @@ function validationFailed(inst, v, cb) {
message = 'is invalid'; message = 'is invalid';
} }
} }
inst.errors.add(attr, message, code); if (kind !== false) inst.errors.add(attr, message, code);
fail = true; fail = true;
}); });
if (cb) { if (cb) {
@ -533,6 +560,7 @@ function skipValidation(inst, conf, kind) {
var defaultMessages = { var defaultMessages = {
presence: 'can\'t be blank', presence: 'can\'t be blank',
absence: 'can\'t be set',
length: { length: {
min: 'too short', min: 'too short',
max: 'too long', max: 'too long',

View File

@ -160,6 +160,20 @@ describe('validations', function () {
}); });
describe('absence', function () {
it('should validate absence', function () {
User.validatesAbsenceOf('reserved', { if: 'locked' });
var u = new User({reserved: 'foo', locked: true});
u.isValid().should.not.be.true;
u.reserved = null;
u.isValid().should.be.true;
var u = new User({reserved: 'foo', locked: false});
u.isValid().should.be.true;
});
});
describe('uniqueness', function () { describe('uniqueness', function () {
it('should validate uniqueness', function (done) { it('should validate uniqueness', function (done) {
User.validatesUniquenessOf('email'); User.validatesUniquenessOf('email');
@ -242,6 +256,18 @@ describe('validations', function () {
}); });
})).should.be.false; })).should.be.false;
}); });
it('should work with if/unless', function (done) {
User.validatesUniquenessOf('email', {
if: function() { return true; },
unless: function() { return false; }
});
var u = new User({email: 'hello'});
Boolean(u.isValid(function (valid) {
valid.should.be.true;
done();
})).should.be.false;
});
}); });
describe('format', function () { describe('format', function () {
@ -266,7 +292,38 @@ describe('validations', function () {
}); });
describe('custom', function () { describe('custom', function () {
it('should validate using custom sync validation'); it('should validate using custom sync validation', function() {
it('should validate using custom async validation'); User.validate('email', function (err) {
if (this.email === 'hello') err();
});
var u = new User({email: 'hello'});
Boolean(u.isValid()).should.be.false;
});
it('should validate and return detailed error messages', function() {
User.validate('global', function (err) {
if (this.email === 'hello' || this.email === 'hey') {
this.errors.add('hello', 'Cannot be `' + this.email + '`', 'invalid');
err(false); // false: prevent global error message
}
});
var u = new User({email: 'hello'});
Boolean(u.isValid()).should.be.false;
u.errors.should.eql({ hello: [ 'Cannot be `hello`' ] });
});
it('should validate using custom async validation', function(done) {
User.validateAsync('email', function (err, next) {
process.nextTick(next);
}, {
if: function() { return true; },
unless: function() { return false; }
});
var u = new User({email: 'hello'});
Boolean(u.isValid(function (valid) {
valid.should.be.true;
done();
})).should.be.false;
});
}); });
}); });