diff --git a/src/object.js b/src/object.js index 6a6964a..0ab690f 100644 --- a/src/object.js +++ b/src/object.js @@ -92,13 +92,15 @@ inherits(ObjectSchema, MixedSchema, { , props = schema._nodes.concat(extra); schema.withMutation(() => { + let innerOptions = { ..._opts, context: {} }; + value = transform(props, function(obj, prop) { var exists = has(value, prop); if (exists && fields[prop]) { var fieldSchema = childSchema(fields[prop], schema.default(undefined)) - obj[prop] = fieldSchema.cast(value[prop], { context: obj }) + obj[prop] = fieldSchema.cast(value[prop], innerOptions) } else if (exists && !strip) obj[prop] = value[prop] @@ -109,7 +111,7 @@ inherits(ObjectSchema, MixedSchema, { if (fieldDefault !== undefined) obj[prop] = fieldDefault } - }, {}) + }, innerOptions.context) delete schema._default }) @@ -201,23 +203,23 @@ inherits(ObjectSchema, MixedSchema, { }) }, - noUnknown(noAllow, message) { - if ( typeof noAllow === 'string') + noUnknown(noAllow = true, message = locale.noUnknown) { + if (typeof noAllow === 'string') message = noAllow, noAllow = true; var next = this.test({ name: 'noUnknown', exclusive: true, - message: message || locale.noUnknown, + message: message, test(value) { return value == null || !noAllow || unknown(this.schema, value).length === 0 } }) - if ( noAllow ) - this._options.stripUnknown = true + if (noAllow) + next._options.stripUnknown = true - return next + return next }, camelcase(){ diff --git a/test/object.js b/test/object.js index 75ad6e7..b728047 100644 --- a/test/object.js +++ b/test/object.js @@ -142,6 +142,24 @@ describe('Object types', function(){ }) + it('should pass options to children', function() { + object({ + names: object({ + first: string() + }) + }) + .cast({ + extra: true, + names: { first: 'john', extra: true } + }, { stripUnknown: true } + ) + .should.eql({ + names: { + first: 'john' + } + }) + }) + it('should call shape with constructed with an arg', function(){ var inst = object({ prop: mixed() @@ -196,20 +214,29 @@ describe('Object types', function(){ prop: mixed(), other: mixed() }) + + return Promise.all([ + inst .noUnknown('hi') + .validate({ extra: 'field' }, { strict: true }).should.be.rejected + .then(function(err){ + err.errors[0].should.equal('hi') + }), - return inst.validate({ extra: 'field' }) - .should.be.rejected - .then(function(err){ - err.errors[0].should.equal('hi') - }) + inst + .noUnknown() + .validate({ extra: 'field' }, { strict: true }).should.be.rejected + .then(function(err){ + err.errors[0].should.be.a('string') + }) + ]) }) it('should handle custom validation', function(){ var inst = object().shape({ - prop: mixed(), - other: mixed() - }) + prop: mixed(), + other: mixed() + }) inst = inst.test('test', '${path} oops', function(){ return false @@ -328,7 +355,7 @@ describe('Object types', function(){ it('should not move keys when it does not exist', function(){ var inst = object().shape({ - myProp: mixed(), + myProp: mixed() }) .from('prop', 'myProp') @@ -386,7 +413,7 @@ describe('Object types', function(){ it('should use correct default when concating', function(){ var inst = object().shape({ - other: bool(), + other: bool() }) .default(undefined)