Skip to content

Commit f7446d2

Browse files
committed
[fixed] pass path correctly to cast()
1 parent 9b5232a commit f7446d2

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

src/object.js

+11-7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { object as locale } from './locale.js';
99
import sortFields from './util/sortFields';
1010
import sortByKeyOrder from './util/sortByKeyOrder';
1111
import inherits from './util/inherits';
12+
import makePath from './util/makePath';
1213
import runValidations, { propagateErrors } from './util/runValidations';
1314

1415
let isObject = obj => Object.prototype.toString.call(obj) === '[object Object]';
@@ -76,8 +77,8 @@ inherits(ObjectSchema, MixedSchema, {
7677
return isObject(value) || typeof value === 'function';
7778
},
7879

79-
_cast(_value, opts = {}) {
80-
var value = MixedSchema.prototype._cast.call(this, _value, opts)
80+
_cast(_value, options = {}) {
81+
var value = MixedSchema.prototype._cast.call(this, _value, options)
8182

8283
//should ignore nulls here
8384
if (value === undefined)
@@ -87,18 +88,18 @@ inherits(ObjectSchema, MixedSchema, {
8788
return value;
8889

8990
var fields = this.fields
90-
, strip = this._option('stripUnknown', opts) === true
91+
, strip = this._option('stripUnknown', options) === true
9192
, extra = Object.keys(value).filter(v => this._nodes.indexOf(v) === -1)
9293
, props = this._nodes.concat(extra);
9394

9495
let innerOptions = {
95-
...opts,
96+
...options,
9697
parent: {}, // is filled during the transform below
9798
__validating: false,
9899
};
99100

100101
value = transform(props, (obj, prop) => {
101-
let field = fields[prop]
102+
let field = fields[prop];
102103
let exists = has(value, prop);
103104

104105
if (field) {
@@ -108,7 +109,10 @@ inherits(ObjectSchema, MixedSchema, {
108109
if (field._strip === true)
109110
return
110111

111-
fieldValue = !opts.__validating || !strict
112+
// should be safe to mutate since this is fired in sequence
113+
innerOptions.path = makePath`${options.path}.${prop}`;
114+
115+
fieldValue = !options.__validating || !strict
112116
? field.cast(value[prop], innerOptions)
113117
: value[prop]
114118

@@ -142,7 +146,7 @@ inherits(ObjectSchema, MixedSchema, {
142146
}
143147

144148
let validations = this._nodes.map(key => {
145-
var path = (opts.path ? (opts.path + '.') : '') + key
149+
var path = makePath`${opts.path}.${key}`
146150
, field = this.fields[key]
147151
, innerOptions = { ...opts, path, parent: value };
148152

0 commit comments

Comments
 (0)