merged isEmpty change to isEmptyValue
This commit is contained in:
commit
9746386092
21
lib/rules.js
21
lib/rules.js
|
@ -30,7 +30,7 @@ Rules.prototype.Error = function(message, rule, value) {
|
|||
};
|
||||
|
||||
Rules.prototype.apply = function(value) {
|
||||
if (_.isEmpty(value) && !_.isUndefined(this.rules['default'])) {
|
||||
if (isValueEmpty(value) && !_.isUndefined(this.rules['default'])) {
|
||||
if ('function' === typeof(this.rules['default'])) {
|
||||
value = this.rules['default'].call();
|
||||
} else {
|
||||
|
@ -254,6 +254,25 @@ var filters = {
|
|||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* _.isEmpty(value) only verifies string arrays and objects.
|
||||
* Other types will be asserted by a type verification.
|
||||
*
|
||||
* @param {mixed} value
|
||||
* @return {Boolean}
|
||||
*/
|
||||
function isValueEmpty(value) {
|
||||
if (_.isUndefined(value)) {
|
||||
return true;
|
||||
}
|
||||
var canUseIsEmpty = _.isString(value) || _.isArray(value) || _.isObject(value) || false;
|
||||
if (canUseIsEmpty) {
|
||||
return _.isEmpty(value);
|
||||
}
|
||||
return (_.isNumber(value) || _.isBoolean(value) || _.isDate(value)) ? false : true;
|
||||
}
|
||||
|
||||
|
||||
exports.create = function(param, rules) {
|
||||
return new Rules(param, rules);
|
||||
};
|
||||
|
|
|
@ -29,4 +29,23 @@ describe("default schemas", function()
|
|||
expect(input1.data.sound).to.equal('meow');
|
||||
expect(input2.data.sound).to.equal('moo moo');
|
||||
});
|
||||
|
||||
it("respects non string/object/array values with defaults", function () {
|
||||
var
|
||||
input1, input2,
|
||||
schema = schemajs.create({
|
||||
counter: {
|
||||
type: 'number',
|
||||
'default': 30
|
||||
}
|
||||
});
|
||||
|
||||
input1 = schema.validate({
|
||||
counter: 9
|
||||
});
|
||||
input2 = schema.validate({});
|
||||
|
||||
expect(input1.data.counter).to.equal(9);
|
||||
expect(input2.data.counter).to.equal(30);
|
||||
});
|
||||
});
|
||||
|
|
Reference in New Issue