Skip to content

Commit

Permalink
fix: array reaching
Browse files Browse the repository at this point in the history
  • Loading branch information
jquense committed Apr 20, 2020
1 parent 48ac4c3 commit 81e4058
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 19 deletions.
11 changes: 9 additions & 2 deletions src/util/reach.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ export function getIn(schema, path, value, context) {
schema,
};

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

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

if (isLast) {
parent = value;
lastPart = part;
lastPartDebug = isBracket ? '[' + _part + ']' : '.' + _part;
}
value = value[idx];
}
}
Expand All @@ -43,7 +50,7 @@ export function getIn(schema, path, value, context) {
if (!has(schema, 'fields') || !has(schema.fields, part))
throw new Error(
`The schema does not contain the path: ${path}. ` +
`(failed at: ${lastPartDebug} which is a type: "${schema._type}") `,
`(failed at: ${lastPartDebug} which is a type: "${schema._type}")`,
);

schema = schema.fields[part];
Expand Down
67 changes: 50 additions & 17 deletions test/yup.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,18 @@ describe('Yup', function() {
});

it('should getIn correctly', async () => {
var num = number(),
inst = object().shape({
num: number().max(4),

nested: object().shape({
arr: array().of(object().shape({ 'num-1': num })),
}),
});
let num = number();
let shape = object({ 'num-1': num });
let inst = object({
num: number().max(4),

nested: object({
arr: array().of(shape),
}),
});

const value = { nested: { arr: [{}, { 'num-1': 2 }] } };
const { schema, parent, parentPath } = getIn(
let { schema, parent, parentPath } = getIn(
inst,
'nested.arr[1].num-1',
value,
Expand All @@ -87,21 +88,50 @@ describe('Yup', function() {
expect(parent).to.equal(value.nested.arr[1]);
});

it('should getIn array correctly', async () => {
let num = number();
let shape = object({ 'num-1': num });
let inst = object({
num: number().max(4),

nested: object({
arr: array().of(shape),
}),
});

const value = {
nested: {
arr: [{}, { 'num-1': 2 }],
},
};

const { schema, parent, parentPath } = getIn(inst, 'nested.arr[1]', value);

console.log(parentPath);
expect(schema).to.equal(shape);
expect(parentPath).to.equal('1');
expect(parent).to.equal(value.nested.arr);
});

it('should REACH correctly', async () => {
var num = number(),
inst = object().shape({
num: number().max(4),
let num = number();
let shape = object({ num });

nested: object().shape({
arr: array().of(object().shape({ num: num })),
}),
});
let inst = object({
num: number().max(4),

nested: object({
arr: array().of(shape),
}),
});

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

reach(inst, 'nested.arr.num').should.equal(num);
reach(inst, 'nested.arr[].num').should.equal(num);
reach(inst, 'nested.arr[1].num').should.equal(num);
reach(inst, 'nested.arr[1]').should.equal(shape);

reach(inst, 'nested["arr"][1].num').should.not.equal(number());

let valid = await reach(inst, 'nested.arr[].num').isValid(5);
Expand Down Expand Up @@ -185,7 +215,10 @@ describe('Yup', function() {
})
.strict()
.validate({
x: [{ type: 1, foo: '4' }, { type: 2, foo: '5' }],
x: [
{ type: 1, foo: '4' },
{ type: 2, foo: '5' },
],
})
.should.be.rejected();
err.message.should.match(/must be a `number` type/);
Expand Down

0 comments on commit 81e4058

Please sign in to comment.