From 7daaf12acf00d0b0ef85588403e6bb6cbd662289 Mon Sep 17 00:00:00 2001 From: Abdelrahman Awad Date: Sat, 7 Aug 2021 13:43:38 +0200 Subject: [PATCH] fix: mapField does not handle array in schema array properly Co-authored-by: kevinjxx --- packages/plugin-vee-validate/src/index.js | 23 ++++-- .../tests/integration/integration.spec.js | 74 +++++++++++++++++++ 2 files changed, 89 insertions(+), 8 deletions(-) diff --git a/packages/plugin-vee-validate/src/index.js b/packages/plugin-vee-validate/src/index.js index 7414b00b..0a14f5c1 100644 --- a/packages/plugin-vee-validate/src/index.js +++ b/packages/plugin-vee-validate/src/index.js @@ -69,15 +69,22 @@ export default function VeeValidatePlugin (opts) { } } - return { - ...el, - // namespaced prop to avoid clash with users' props - _veeValidateConfig: { - mapProps, - path - }, - component: withField(el) + const constructField = (field) => { + return { + ...field, + _veeValidateConfig: { + mapProps, + path + }, + component: withField(field) + } } + + if (Array.isArray(el)) { + return el.map(constructField) + } + + return constructField(el) } // Map components in schema to enhanced versions with `useField` diff --git a/packages/plugin-vee-validate/tests/integration/integration.spec.js b/packages/plugin-vee-validate/tests/integration/integration.spec.js index b52d77ea..08cb4a66 100644 --- a/packages/plugin-vee-validate/tests/integration/integration.spec.js +++ b/packages/plugin-vee-validate/tests/integration/integration.spec.js @@ -657,4 +657,78 @@ describe('FVL integration', () => { expect(button.element.disabled).toBe(false) expect(wrapper.find('#error').text()).toBe('') }) + + it('validates fields with array in nested array schema', async () => { + const SchemaWithValidation = SchemaFormFactory([veeValidatePlugin()]) + const schema = { + user: { + component: SchemaWithValidation, + model: 'subform', + schema: [ + { + model: 'email', + label: 'Email', + component: FormText, + validations: yup.string().required(REQUIRED_MESSAGE) + }, [ + { + model: 'firstName', + label: 'First Name', + component: FormText, + validations: yup.string().required(REQUIRED_MESSAGE) + }, { + model: 'lastName', + label: 'Last Name', + component: FormText, + validations: yup.string().required(REQUIRED_MESSAGE) + } + ] + ] + } + } + + const wrapper = mount({ + template: ` + + `, + components: { + SchemaWithValidation + }, + setup () { + const formData = ref({}) + useSchemaForm(formData) + + return { + schema + } + } + }) + + const inputs = wrapper.findAllComponents(FormText) + const errors = wrapper.findAll('span') + + inputs[0].setValue('') + await flushPromises() + expect(errors[0].text()).toBe(REQUIRED_MESSAGE) + + inputs[0].setValue('test@gmail.com') + await flushPromises() + expect(errors[0].text()).toBe('') + + inputs[1].setValue('') + await flushPromises() + expect(errors[1].text()).toBe(REQUIRED_MESSAGE) + + inputs[1].setValue('Tom') + await flushPromises() + expect(errors[1].text()).toBe('') + + inputs[2].setValue('') + await flushPromises() + expect(errors[2].text()).toBe(REQUIRED_MESSAGE) + + inputs[2].setValue('Cruise') + await flushPromises() + expect(errors[2].text()).toBe('') + }) })