Skip to content

Commit 81e4058

Browse files
committedApr 20, 2020
fix: array reaching
1 parent 48ac4c3 commit 81e4058

File tree

2 files changed

+59
-19
lines changed

2 files changed

+59
-19
lines changed
 

‎src/util/reach.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ export function getIn(schema, path, value, context) {
1616
schema,
1717
};
1818

19-
forEach(path, (_part, isBracket, isArray) => {
19+
// I no longer remember why this is so complicated
20+
forEach(path, (_part, isBracket, isArray, partIdx, parts) => {
21+
let isLast = partIdx === parts.length - 1;
2022
let part = isBracket ? trim(_part) : _part;
2123

2224
if (isArray || has(schema, '_subType')) {
@@ -33,6 +35,11 @@ export function getIn(schema, path, value, context) {
3335
);
3436
}
3537

38+
if (isLast) {
39+
parent = value;
40+
lastPart = part;
41+
lastPartDebug = isBracket ? '[' + _part + ']' : '.' + _part;
42+
}
3643
value = value[idx];
3744
}
3845
}
@@ -43,7 +50,7 @@ export function getIn(schema, path, value, context) {
4350
if (!has(schema, 'fields') || !has(schema.fields, part))
4451
throw new Error(
4552
`The schema does not contain the path: ${path}. ` +
46-
`(failed at: ${lastPartDebug} which is a type: "${schema._type}") `,
53+
`(failed at: ${lastPartDebug} which is a type: "${schema._type}")`,
4754
);
4855

4956
schema = schema.fields[part];

‎test/yup.js

+50-17
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,18 @@ describe('Yup', function() {
6666
});
6767

6868
it('should getIn correctly', async () => {
69-
var num = number(),
70-
inst = object().shape({
71-
num: number().max(4),
72-
73-
nested: object().shape({
74-
arr: array().of(object().shape({ 'num-1': num })),
75-
}),
76-
});
69+
let num = number();
70+
let shape = object({ 'num-1': num });
71+
let inst = object({
72+
num: number().max(4),
73+
74+
nested: object({
75+
arr: array().of(shape),
76+
}),
77+
});
7778

7879
const value = { nested: { arr: [{}, { 'num-1': 2 }] } };
79-
const { schema, parent, parentPath } = getIn(
80+
let { schema, parent, parentPath } = getIn(
8081
inst,
8182
'nested.arr[1].num-1',
8283
value,
@@ -87,21 +88,50 @@ describe('Yup', function() {
8788
expect(parent).to.equal(value.nested.arr[1]);
8889
});
8990

91+
it('should getIn array correctly', async () => {
92+
let num = number();
93+
let shape = object({ 'num-1': num });
94+
let inst = object({
95+
num: number().max(4),
96+
97+
nested: object({
98+
arr: array().of(shape),
99+
}),
100+
});
101+
102+
const value = {
103+
nested: {
104+
arr: [{}, { 'num-1': 2 }],
105+
},
106+
};
107+
108+
const { schema, parent, parentPath } = getIn(inst, 'nested.arr[1]', value);
109+
110+
console.log(parentPath);
111+
expect(schema).to.equal(shape);
112+
expect(parentPath).to.equal('1');
113+
expect(parent).to.equal(value.nested.arr);
114+
});
115+
90116
it('should REACH correctly', async () => {
91-
var num = number(),
92-
inst = object().shape({
93-
num: number().max(4),
117+
let num = number();
118+
let shape = object({ num });
94119

95-
nested: object().shape({
96-
arr: array().of(object().shape({ num: num })),
97-
}),
98-
});
120+
let inst = object({
121+
num: number().max(4),
122+
123+
nested: object({
124+
arr: array().of(shape),
125+
}),
126+
});
99127

100128
reach(inst, '').should.equal(inst);
101129

102130
reach(inst, 'nested.arr.num').should.equal(num);
103131
reach(inst, 'nested.arr[].num').should.equal(num);
104132
reach(inst, 'nested.arr[1].num').should.equal(num);
133+
reach(inst, 'nested.arr[1]').should.equal(shape);
134+
105135
reach(inst, 'nested["arr"][1].num').should.not.equal(number());
106136

107137
let valid = await reach(inst, 'nested.arr[].num').isValid(5);
@@ -185,7 +215,10 @@ describe('Yup', function() {
185215
})
186216
.strict()
187217
.validate({
188-
x: [{ type: 1, foo: '4' }, { type: 2, foo: '5' }],
218+
x: [
219+
{ type: 1, foo: '4' },
220+
{ type: 2, foo: '5' },
221+
],
189222
})
190223
.should.be.rejected();
191224
err.message.should.match(/must be a `number` type/);

0 commit comments

Comments
 (0)