Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix array path resolve for descendants #669

Merged
merged 3 commits into from
Oct 24, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,13 @@ 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) => {
var path = makePath`${_opts.path}[${idx}]`;
var innerOptions = {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you move this inline below: cast(v, { ..._opts, path: makePat

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for quick check @jquense ! Made the change.

..._opts,
path,
};
const castElement = this._subType.cast(v, innerOptions);
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);
});
});