fixed merge conflicts

This commit is contained in:
Keith Abdulla 2014-08-23 23:19:11 -07:00
commit b3d795b2a7
3 changed files with 57 additions and 23 deletions

View File

@ -30,8 +30,12 @@ Rules.prototype.Error = function(message, rule, value) {
};
Rules.prototype.apply = function(value) {
if (_.isEmpty(value) && !_.isUndefined(this.rules['default'])) {
value = this.rules['default'];
if (isValueEmpty(value) && !_.isUndefined(this.rules['default'])) {
if ('function' === typeof(this.rules['default'])) {
value = this.rules['default'].call();
} else {
value = this.rules['default'];
}
}
if (this.rules.required) {
@ -250,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);
};

View File

@ -5,27 +5,7 @@ var Schema = function(schema) {
this.schema = schema;
};
var checkObject = function(schema, key, errors, values, get) {
var value;
_.keys(schema.schema).forEach(function(param) {
try {
// if undefined, don't store it
value = rules.create(key + "." + param, schema.schema[param]).apply(get(key)[param]);
if (!_.isUndefined(value)) {
values[key][param] = value;
}
} catch (error) {
if (!errors[key] || typeof(errors[key]) != 'object') errors[key] = {};
errors[key][param] = error;
}
});
};
var checkArray = function(schema, key, errors, values, get) {
var checkArray = function(schema, key, errors, values) {
var valid = true;
values.forEach(function(value, index) {
try {

View File

@ -17,4 +17,35 @@ describe("default schemas", function()
expect(input1.data.sound).to.equal("meow");
expect(input2.data.sound).to.equal("mooo");
});
it('should execute a function supplied for default', function () {
var schema = schemajs.create({
sound: { type: 'string', 'default': function () { return 'moo moo'; } }
});
var input1 = schema.validate({ sound: 'meow' });
var input2 = schema.validate({});
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);
});
});