Skip to content

Commit e1d4891

Browse files
committed
[fixed] don't alias non existent fields
1 parent 5d65038 commit e1d4891

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

src/object.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -178,13 +178,17 @@ inherits(ObjectSchema, MixedSchema, {
178178

179179
from(from, to, alias) {
180180
return this.transform( obj => {
181-
if ( obj == null)
181+
var newObj = obj;
182+
183+
if (obj == null)
182184
return obj
183185

184-
var newObj = transform(obj, (o, val, key) => key !== from && (o[key] = val), {})
186+
if (has(obj, from)) {
187+
newObj = transform(obj, (o, val, key) => key !== from && (o[key] = val), {})
188+
newObj[to] = obj[from]
185189

186-
newObj[to] = obj[from]
187-
if(alias) newObj[from] = obj[from]
190+
if(alias) newObj[from] = obj[from]
191+
}
188192

189193
return newObj
190194
})
@@ -248,4 +252,4 @@ function sortFields(fields, excludes = []){
248252
}
249253

250254
return toposort.array(nodes, edges).reverse()
251-
}
255+
}

test/object.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,19 @@ describe('Object types', function(){
291291
.should.eql({ myProp: 5, other: 6, Other: 6 })
292292
})
293293

294+
it('should not move keys when it does not exist', function(){
295+
var inst = object().shape({
296+
myProp: mixed(),
297+
})
298+
.from('prop', 'myProp')
299+
300+
inst.cast({ myProp: 5 })
301+
.should.eql({ myProp: 5 })
302+
303+
inst.cast({ myProp: 5, prop: 7 })
304+
.should.eql({ myProp: 7 })
305+
})
306+
294307
it('should handle conditionals', function(){
295308
var inst = object().shape({
296309
noteDate: number()
@@ -415,4 +428,4 @@ describe('Object types', function(){
415428
})
416429

417430

418-
})
431+
})

0 commit comments

Comments
 (0)