diff --git a/src/array.js b/src/array.js index c05396304..e66fead8f 100644 --- a/src/array.js +++ b/src/array.js @@ -46,8 +46,11 @@ inherits(ArraySchema, MixedSchema, { if (!this._typeCheck(value) || !this._subType) return value; let isChanged = false; - const castArray = value.map(v => { - const castElement = this._subType.cast(v, _opts); + const castArray = value.map((v, idx) => { + const castElement = this._subType.cast(v, { + ..._opts, + path: makePath`${_opts.path}[${idx}]`, + }); if (castElement !== v) { isChanged = true; } diff --git a/test/array.js b/test/array.js index ff303ef99..5c3bec587 100644 --- a/test/array.js +++ b/test/array.js @@ -175,4 +175,19 @@ describe('Array types', () => { inst.cast(null).should.eql([]); }); + + it('should pass resolved path to descendants', async () => { + let value = ['2', '3']; + let expectedPaths = ['[0]', '[1]']; + + let itemSchema = string().when([], function(_, context) { + let path = context.path || ''; + path.should.be.oneOf(expectedPaths); + return string().required(); + }); + + await array() + .of(itemSchema) + .validate(value); + }); });