Skip to content

Commit

Permalink
[fixed] don't alias non existent fields
Browse files Browse the repository at this point in the history
  • Loading branch information
jquense committed Oct 29, 2015
1 parent 5d65038 commit e1d4891
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
14 changes: 9 additions & 5 deletions src/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,17 @@ inherits(ObjectSchema, MixedSchema, {

from(from, to, alias) {
return this.transform( obj => {
if ( obj == null)
var newObj = obj;

if (obj == null)
return obj

var newObj = transform(obj, (o, val, key) => key !== from && (o[key] = val), {})
if (has(obj, from)) {
newObj = transform(obj, (o, val, key) => key !== from && (o[key] = val), {})
newObj[to] = obj[from]

newObj[to] = obj[from]
if(alias) newObj[from] = obj[from]
if(alias) newObj[from] = obj[from]
}

return newObj
})
Expand Down Expand Up @@ -248,4 +252,4 @@ function sortFields(fields, excludes = []){
}

return toposort.array(nodes, edges).reverse()
}
}
15 changes: 14 additions & 1 deletion test/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,19 @@ describe('Object types', function(){
.should.eql({ myProp: 5, other: 6, Other: 6 })
})

it('should not move keys when it does not exist', function(){
var inst = object().shape({
myProp: mixed(),
})
.from('prop', 'myProp')

inst.cast({ myProp: 5 })
.should.eql({ myProp: 5 })

inst.cast({ myProp: 5, prop: 7 })
.should.eql({ myProp: 7 })
})

it('should handle conditionals', function(){
var inst = object().shape({
noteDate: number()
Expand Down Expand Up @@ -415,4 +428,4 @@ describe('Object types', function(){
})


})
})

0 comments on commit e1d4891

Please sign in to comment.