Skip to content

Commit

Permalink
[fixed] noUnknown and stripUnknown work and propagate to children
Browse files Browse the repository at this point in the history
  • Loading branch information
jquense committed Feb 16, 2016
1 parent 4baf89e commit ff19720
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 18 deletions.
18 changes: 10 additions & 8 deletions src/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -109,7 +111,7 @@ inherits(ObjectSchema, MixedSchema, {
if (fieldDefault !== undefined)
obj[prop] = fieldDefault
}
}, {})
}, innerOptions.context)

delete schema._default
})
Expand Down Expand Up @@ -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(){
Expand Down
47 changes: 37 additions & 10 deletions test/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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')

Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit ff19720

Please sign in to comment.