Merge pull request #1432 from simoami/master

#1386 Allow empty values when allowBlank is true
This commit is contained in:
Diana Lau 2017-07-20 11:23:54 -04:00 committed by GitHub
commit ff9b25a905
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() {