Skip to content

Commit 431a2da

Browse files
committed
added support for array nested default functions
1 parent e4ae788 commit 431a2da

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

lib/schema.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,17 @@ Schema.prototype.validate = function (params, options) {
174174
};
175175

176176
internals.invokeDefaultFunctions = function (data) {
177-
return _.mapValues(data, function (val) {
177+
var mapper;
178+
if (_.isPlainObject(data)) {
179+
mapper = _.mapValues;
180+
} else {
181+
mapper = _.map;
182+
}
183+
184+
return mapper(data, function (val) {
178185
if(_.isFunction(val)) {
179186
return val.call(null);
180-
} else if (_.isPlainObject(val)) {
187+
} else if (_.isPlainObject(val) || _.isArray(val)) {
181188
return internals.invokeDefaultFunctions(val);
182189
} else {
183190
return val;

test/schema-test.js

+31
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,37 @@ describe('schema', function () {
470470
clock.restore();
471471
});
472472

473+
it('should return result of nested default functions', function () {
474+
var clock = sinon.useFakeTimers(Date.now());
475+
476+
var config = {
477+
hashKey : 'email',
478+
schema : {
479+
email : Joi.string(),
480+
created : Joi.date().default(Date.now),
481+
properties : Joi.array().includes(Joi.object().keys({
482+
created : Joi.date().default(Date.now),
483+
name : Joi.string().default('Tim Testers property')
484+
}))
485+
}
486+
};
487+
488+
var s = new Schema(config);
489+
490+
var d = s.applyDefaults({email: '[email protected]', properties: [ {} ] });
491+
492+
d.should.eql({
493+
email : '[email protected]',
494+
created : Date.now(),
495+
properties : [{
496+
created : Date.now(),
497+
name : 'Tim Testers property'
498+
}]
499+
});
500+
501+
clock.restore();
502+
});
503+
473504
});
474505

475506
});

0 commit comments

Comments
 (0)