Skip to content

Commit 9420ce7

Browse files
author
Larry Botha
committed
fix(isvalid store): fix isValid returning 'false' for valid nested arrays
fix tjinauyeung#115
1 parent 22f948a commit 9420ce7

File tree

2 files changed

+66
-3
lines changed

2 files changed

+66
-3
lines changed

lib/util.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,14 @@ function isEmpty(object) {
2626
}
2727

2828
function getValues(object) {
29-
let result = [];
29+
let results = [];
30+
3031
for (const [, value] of Object.entries(object)) {
31-
result = [...result, ...(typeof value === 'object' ? getValues(value) : [value])];
32+
const values = typeof value === 'object' ? getValues(value) : [value];
33+
results = [...results, ...values];
3234
}
33-
return result;
35+
36+
return results;
3437
}
3538

3639
// TODO: refactor this so as not to rely directly on yup's API

test/specs/library.spec.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,66 @@ describe('createForm', () => {
255255
})
256256
.then(done);
257257
});
258+
259+
it('is false for invalid arrays', async done => {
260+
const validationSchema = yup
261+
.array()
262+
.of(yup.object().shape({x: yup.string().required()}).required());
263+
const initialValues = [{x: ''}];
264+
const formInstance = getInstance({validationSchema, initialValues});
265+
266+
formInstance
267+
.handleSubmit()
268+
.then(() => subscribeOnce(formInstance.isValid))
269+
.then(isValid => expect(isValid).toBe(false))
270+
.then(done);
271+
});
272+
273+
it('is true for valid arrays', async done => {
274+
const validationSchema = yup
275+
.array()
276+
.of(yup.object().shape({x: yup.string().required()}).required());
277+
const initialValues = [{x: 'foo'}];
278+
const formInstance = getInstance({validationSchema, initialValues});
279+
280+
formInstance
281+
.handleSubmit()
282+
.then(() => subscribeOnce(formInstance.isValid))
283+
.then(isValid => expect(isValid).toBe(true))
284+
.then(done);
285+
});
286+
287+
it('is false for invalid nested arrays', async done => {
288+
const validationSchema = yup.object().shape({
289+
xs: yup
290+
.array()
291+
.of(yup.object().shape({x: yup.string().required()}).required()),
292+
});
293+
const initialValues = {xs: [{x: ''}]};
294+
const formInstance = getInstance({validationSchema, initialValues});
295+
296+
formInstance
297+
.handleSubmit()
298+
.then(() => subscribeOnce(formInstance.isValid))
299+
.then(isValid => expect(isValid).toBe(false))
300+
.then(done);
301+
});
302+
303+
it('is true for valid nested arrays', async done => {
304+
const validationSchema = yup.object().shape({
305+
xs: yup
306+
.array()
307+
.of(yup.object().shape({x: yup.string().required()}).required()),
308+
});
309+
const initialValues = {xs: [{x: 'bar'}]};
310+
const formInstance = getInstance({validationSchema, initialValues});
311+
312+
formInstance
313+
.handleSubmit()
314+
.then(() => subscribeOnce(formInstance.isValid))
315+
.then(isValid => expect(isValid).toBe(true))
316+
.then(done);
317+
});
258318
});
259319

260320
describe('handleReset', () => {

0 commit comments

Comments
 (0)