From 9420ce770c759ef97a55b16d773f343d6afbcb96 Mon Sep 17 00:00:00 2001 From: Larry Botha Date: Sat, 1 May 2021 14:59:19 +0200 Subject: [PATCH] fix(isvalid store): fix isValid returning 'false' for valid nested arrays fix #115 --- lib/util.js | 9 ++++-- test/specs/library.spec.js | 60 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 3 deletions(-) diff --git a/lib/util.js b/lib/util.js index da2bbf0..8b37b36 100644 --- a/lib/util.js +++ b/lib/util.js @@ -26,11 +26,14 @@ function isEmpty(object) { } function getValues(object) { - let result = []; + let results = []; + for (const [, value] of Object.entries(object)) { - result = [...result, ...(typeof value === 'object' ? getValues(value) : [value])]; + const values = typeof value === 'object' ? getValues(value) : [value]; + results = [...results, ...values]; } - return result; + + return results; } // TODO: refactor this so as not to rely directly on yup's API diff --git a/test/specs/library.spec.js b/test/specs/library.spec.js index 46f2312..7a96170 100644 --- a/test/specs/library.spec.js +++ b/test/specs/library.spec.js @@ -255,6 +255,66 @@ describe('createForm', () => { }) .then(done); }); + + it('is false for invalid arrays', async done => { + const validationSchema = yup + .array() + .of(yup.object().shape({x: yup.string().required()}).required()); + const initialValues = [{x: ''}]; + const formInstance = getInstance({validationSchema, initialValues}); + + formInstance + .handleSubmit() + .then(() => subscribeOnce(formInstance.isValid)) + .then(isValid => expect(isValid).toBe(false)) + .then(done); + }); + + it('is true for valid arrays', async done => { + const validationSchema = yup + .array() + .of(yup.object().shape({x: yup.string().required()}).required()); + const initialValues = [{x: 'foo'}]; + const formInstance = getInstance({validationSchema, initialValues}); + + formInstance + .handleSubmit() + .then(() => subscribeOnce(formInstance.isValid)) + .then(isValid => expect(isValid).toBe(true)) + .then(done); + }); + + it('is false for invalid nested arrays', async done => { + const validationSchema = yup.object().shape({ + xs: yup + .array() + .of(yup.object().shape({x: yup.string().required()}).required()), + }); + const initialValues = {xs: [{x: ''}]}; + const formInstance = getInstance({validationSchema, initialValues}); + + formInstance + .handleSubmit() + .then(() => subscribeOnce(formInstance.isValid)) + .then(isValid => expect(isValid).toBe(false)) + .then(done); + }); + + it('is true for valid nested arrays', async done => { + const validationSchema = yup.object().shape({ + xs: yup + .array() + .of(yup.object().shape({x: yup.string().required()}).required()), + }); + const initialValues = {xs: [{x: 'bar'}]}; + const formInstance = getInstance({validationSchema, initialValues}); + + formInstance + .handleSubmit() + .then(() => subscribeOnce(formInstance.isValid)) + .then(isValid => expect(isValid).toBe(true)) + .then(done); + }); }); describe('handleReset', () => {