Merge pull request #1109 from C3realGuy/dev_validations

validateNumericality should skip undefined
This commit is contained in:
Amirali Jafarian 2016-10-20 07:37:52 -04:00 committed by GitHub
commit d9a736a4a8
2 changed files with 76 additions and 8 deletions

View File

@ -287,7 +287,7 @@ function validateLength(attr, conf, err, options) {
function validateNumericality(attr, conf, err, options) { function validateNumericality(attr, conf, err, options) {
if (nullCheck.call(this, attr, conf, err)) return; if (nullCheck.call(this, attr, conf, err)) return;
if (typeof this[attr] !== 'number') { if (typeof this[attr] !== 'number' || isNaN(this[attr])) {
return err('number'); return err('number');
} }
if (conf.int && this[attr] !== Math.round(this[attr])) { if (conf.int && this[attr] !== Math.round(this[attr])) {
@ -633,16 +633,26 @@ var defaultMessages = {
uniqueness: 'is not unique', uniqueness: 'is not unique',
}; };
/**
* Checks if attribute is undefined or null. Calls err function with 'blank' or 'null'.
* See defaultMessages. You can affect this behaviour with conf.allowBlank and conf.allowNull.
* @param {String} attr Property name of attribute
* @param {Object} conf conf object for validator
* @param {Function} err
* @return {Boolean} returns true if attribute is null or blank
*/
function nullCheck(attr, conf, err) { function nullCheck(attr, conf, err) {
if (this[attr] == null) { // First determine if attribute is defined
if (!conf.allowNull) { if (typeof this[attr] === 'undefined') {
err('null'); if (!conf.allowBlank) {
err('blank');
} }
return true; return true;
} else { } else {
if (blank(this[attr])) { // Now check if attribute is null
if (!conf.allowBlank) { if (this[attr] === null) {
err('blank'); if (!conf.allowNull) {
err('null');
} }
return true; return true;
} }

View File

@ -558,7 +558,65 @@ describe('validations', function() {
}); });
describe('numericality', function() { describe('numericality', function() {
it('should validate numericality'); it('passes when given numeric values', function() {
User.validatesNumericalityOf('age');
var user = new User({age: 10});
user.isValid().should.be.true();
});
it('fails when given non-numeric values', function() {
User.validatesNumericalityOf('age');
var user = new User({age: 'notanumber'});
user.isValid().should.be.false();
user.errors.should.eql({age: ['is not a number']});
});
it('fails when given undefined values', function() {
User.validatesNumericalityOf('age');
var u = new User({});
u.isValid().should.be.false();
u.errors.should.eql({age: ['is blank']});
});
it('skips undefined values when allowBlank option is true', function() {
User.validatesNumericalityOf('age', {allowBlank: true});
var user = new User({});
user.isValid().should.be.true();
});
it('fails when given non-numeric values when allowBlank option is true', function() {
User.validatesNumericalityOf('age', {allowBlank: true});
var user = new User({age: 'test'});
user.isValid().should.be.false();
user.errors.should.eql({age: ['is not a number']});
});
it('fails when given null values', function() {
User.validatesNumericalityOf('age');
var user = new User({age: null});
user.isValid().should.be.false();
user.errors.should.eql({age: ['is null']});
});
it('passes when given null values when allowNull option is true', function() {
User.validatesNumericalityOf('age', {allowNull: true});
var user = new User({age: null});
user.isValid().should.be.true();
});
it('passes when given float values', function() {
User.validatesNumericalityOf('age');
var user = new User({age: 13.37});
user.isValid().should.be.true();
});
it('fails when given non-integer values when int option is true', function() {
User.validatesNumericalityOf('age', {int: true});
var user = new User({age: 13.37});
user.isValid().should.be.false();
user.errors.should.eql({age: ['is not an integer']});
});
}); });
describe('inclusion', function() { describe('inclusion', function() {