#1386 Allow empty values when allowBlank is true

This commit is contained in:
Simo Moujami 2017-07-19 19:26:13 -04:00
parent 6a1b55568c
commit 82a8e3d9a1
2 changed files with 20 additions and 1 deletions

View File

@ -709,7 +709,7 @@ var defaultMessages = {
*/
function nullCheck(attr, conf, err) {
// First determine if attribute is defined
if (typeof this[attr] === 'undefined') {
if (typeof this[attr] === 'undefined' || this[attr] === '') {
if (!conf.allowBlank) {
err('blank');
}

View File

@ -755,6 +755,25 @@ describe('validations', function() {
done();
});
});
it('passes with an empty value when allowBlank option is true', function(done) {
User.validatesInclusionOf('gender', {in: ['male', 'female'], allowBlank: true});
User.create({gender: ''}, done);
});
it('fails with an empty value when allowBlank option is false', function(done) {
User.validatesInclusionOf('gender', {in: ['male', 'female'], allowBlank: false});
User.create({gender: ''}, function(err) {
err.should.be.instanceOf(ValidationError);
getErrorDetails(err)
.should.equal('`gender` is blank (value: "").');
done();
});
});
function getErrorDetails(err) {
return err.message.replace(/^.*Details: /, '');
}
});
describe('exclusion', function() {