Skip to content

Commit ff19720

Browse files
committed
[fixed] noUnknown and stripUnknown work and propagate to children
1 parent 4baf89e commit ff19720

File tree

2 files changed

+47
-18
lines changed

2 files changed

+47
-18
lines changed

src/object.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,15 @@ inherits(ObjectSchema, MixedSchema, {
9292
, props = schema._nodes.concat(extra);
9393

9494
schema.withMutation(() => {
95+
let innerOptions = { ..._opts, context: {} };
96+
9597
value = transform(props, function(obj, prop) {
9698
var exists = has(value, prop);
9799

98100
if (exists && fields[prop]) {
99101
var fieldSchema = childSchema(fields[prop], schema.default(undefined))
100102

101-
obj[prop] = fieldSchema.cast(value[prop], { context: obj })
103+
obj[prop] = fieldSchema.cast(value[prop], innerOptions)
102104
}
103105
else if (exists && !strip)
104106
obj[prop] = value[prop]
@@ -109,7 +111,7 @@ inherits(ObjectSchema, MixedSchema, {
109111
if (fieldDefault !== undefined)
110112
obj[prop] = fieldDefault
111113
}
112-
}, {})
114+
}, innerOptions.context)
113115

114116
delete schema._default
115117
})
@@ -201,23 +203,23 @@ inherits(ObjectSchema, MixedSchema, {
201203
})
202204
},
203205

204-
noUnknown(noAllow, message) {
205-
if ( typeof noAllow === 'string')
206+
noUnknown(noAllow = true, message = locale.noUnknown) {
207+
if (typeof noAllow === 'string')
206208
message = noAllow, noAllow = true;
207209

208210
var next = this.test({
209211
name: 'noUnknown',
210212
exclusive: true,
211-
message: message || locale.noUnknown,
213+
message: message,
212214
test(value) {
213215
return value == null || !noAllow || unknown(this.schema, value).length === 0
214216
}
215217
})
216218

217-
if ( noAllow )
218-
this._options.stripUnknown = true
219+
if (noAllow)
220+
next._options.stripUnknown = true
219221

220-
return next
222+
return next
221223
},
222224

223225
camelcase(){

test/object.js

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,24 @@ describe('Object types', function(){
142142
})
143143

144144

145+
it('should pass options to children', function() {
146+
object({
147+
names: object({
148+
first: string()
149+
})
150+
})
151+
.cast({
152+
extra: true,
153+
names: { first: 'john', extra: true }
154+
}, { stripUnknown: true }
155+
)
156+
.should.eql({
157+
names: {
158+
first: 'john'
159+
}
160+
})
161+
})
162+
145163
it('should call shape with constructed with an arg', function(){
146164
var inst = object({
147165
prop: mixed()
@@ -196,20 +214,29 @@ describe('Object types', function(){
196214
prop: mixed(),
197215
other: mixed()
198216
})
217+
218+
return Promise.all([
219+
inst
199220
.noUnknown('hi')
221+
.validate({ extra: 'field' }, { strict: true }).should.be.rejected
222+
.then(function(err){
223+
err.errors[0].should.equal('hi')
224+
}),
200225

201-
return inst.validate({ extra: 'field' })
202-
.should.be.rejected
203-
.then(function(err){
204-
err.errors[0].should.equal('hi')
205-
})
226+
inst
227+
.noUnknown()
228+
.validate({ extra: 'field' }, { strict: true }).should.be.rejected
229+
.then(function(err){
230+
err.errors[0].should.be.a('string')
231+
})
232+
])
206233
})
207234

208235
it('should handle custom validation', function(){
209236
var inst = object().shape({
210-
prop: mixed(),
211-
other: mixed()
212-
})
237+
prop: mixed(),
238+
other: mixed()
239+
})
213240

214241
inst = inst.test('test', '${path} oops', function(){
215242
return false
@@ -328,7 +355,7 @@ describe('Object types', function(){
328355

329356
it('should not move keys when it does not exist', function(){
330357
var inst = object().shape({
331-
myProp: mixed(),
358+
myProp: mixed()
332359
})
333360
.from('prop', 'myProp')
334361

@@ -386,7 +413,7 @@ describe('Object types', function(){
386413

387414
it('should use correct default when concating', function(){
388415
var inst = object().shape({
389-
other: bool(),
416+
other: bool()
390417
})
391418
.default(undefined)
392419

0 commit comments

Comments
 (0)