Skip to content

Commit

Permalink
fix: array path resolve for descendants (#669)
Browse files Browse the repository at this point in the history
* fix: array when context path

* test: array resolved context path

* use inline syntax
  • Loading branch information
serhii-ohorodnyk authored and jquense committed Oct 24, 2019
1 parent 0d14827 commit d31e34d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
15 changes: 15 additions & 0 deletions test/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

0 comments on commit d31e34d

Please sign in to comment.