Add tests for validatesExclusionOf (#1248)

This commit is contained in:
Rémi Bèges 2017-03-08 16:24:18 +01:00 committed by Sakib Hasan
parent 76c73d55d2
commit 11c2a19ad2
2 changed files with 51 additions and 3 deletions

View File

@ -144,7 +144,7 @@ Validatable.validatesInclusionOf = getConfigurator('inclusion');
* *
* @param {String} propertyName Property name to validate. * @param {String} propertyName Property name to validate.
* @options {Object} Options * @options {Object} Options
* @property {Array} inArray Property must match one of the values in the array to be valid. * @property {Array} in Property must not match any of the values in the array to be valid.
* @property {String} message Optional error message if property is not valid. Default error message: "is reserved". * @property {String} message Optional error message if property is not valid. Default error message: "is reserved".
* @property {Boolean} allowNull Whether null values are allowed. * @property {Boolean} allowNull Whether null values are allowed.
*/ */

View File

@ -605,7 +605,7 @@ describe('validations', function() {
User.validatesNumericalityOf('age', {int: true}); User.validatesNumericalityOf('age', {int: true});
var user = new User({age: 13.37}); var user = new User({age: 13.37});
user.isValid().should.be.false(); user.isValid().should.be.false();
user.errors.should.eql({age: ['is not an integer']}); user.errors.should.match({age: /is not an integer/});
}); });
}); });
@ -614,7 +614,55 @@ describe('validations', function() {
}); });
describe('exclusion', function() { describe('exclusion', function() {
it('should validate exclusion'); it('fails when excluded value is used for property', function(done) {
User.validatesExclusionOf('name', {in: ['bob']});
User.create({name: 'bob'}, function(err, user) {
err.should.be.instanceof(Error);
err.details.messages.should.match({name: /is reserved/});
done();
});
});
it('passes when excluded value not found for property', function(done) {
User.validatesExclusionOf('name', {in: ['dude']});
User.create({name: 'bob'}, function(err, user) {
if (err) return done(err);
user.name.should.eql('bob');
done();
});
});
it('fails with a custom error message', function(done) {
User.validatesExclusionOf('name', {in: ['bob'], message: 'cannot use this'});
User.create({name: 'bob'}, function(err) {
err.should.be.instanceof(Error);
err.details.messages.should.match({name: /cannot use this/});
done();
});
});
it('fails with a null value when allowNull is false', function(done) {
User.validatesExclusionOf('name', {in: ['bob'], allowNull: false});
User.create({name: null}, function(err) {
err.should.be.instanceof(Error);
err.details.messages.should.match({name: /is null/});
done();
});
});
it('passes with a null value when allowNull is true', function(done) {
User.validatesExclusionOf('name', {in: ['bob'], allowNull: true});
User.create({name: null}, done);
});
it('fails if value is used for integer property', function(done) {
User.validatesExclusionOf('age', {in: [123, 456]});
User.create({age: 123}, function(err) {
err.should.be.instanceof(Error);
err.details.messages.should.match({age: /is reserved/});
done();
});
});
}); });
describe('length', function() { describe('length', function() {