Merge pull request #409 from billinghamj/master

Fixed nullCheck in validations for undefined attributes
This commit is contained in:
Raymond Feng 2015-01-27 14:20:53 -08:00
commit 577def97f8
2 changed files with 25 additions and 2 deletions

View File

@ -589,8 +589,7 @@ var defaultMessages = {
}; };
function nullCheck(attr, conf, err) { function nullCheck(attr, conf, err) {
var isNull = this[attr] === null || !(attr in this); if (this[attr] == null) {
if (isNull) {
if (!conf.allowNull) { if (!conf.allowNull) {
err('null'); err('null');
} }

View File

@ -387,6 +387,30 @@ describe('validations', function () {
describe('format', function () { describe('format', function () {
it('should validate format'); it('should validate format');
it('should overwrite default blank message with custom format message'); it('should overwrite default blank message with custom format message');
it('should skip missing values when allowing null', function () {
User.validatesFormatOf('email', { with: /^\S+@\S+\.\S+$/, allowNull: true });
var u = new User({});
u.isValid().should.be.true;
});
it('should skip null values when allowing null', function () {
User.validatesFormatOf('email', { with: /^\S+@\S+\.\S+$/, allowNull: true });
var u = new User({ email: null });
u.isValid().should.be.true;
});
it('should not skip missing values', function () {
User.validatesFormatOf('email', { with: /^\S+@\S+\.\S+$/ });
var u = new User({});
u.isValid().should.be.false;
});
it('should not skip null values', function () {
User.validatesFormatOf('email', { with: /^\S+@\S+\.\S+$/ });
var u = new User({ email: null });
u.isValid().should.be.false;
});
}); });
describe('numericality', function () { describe('numericality', function () {