Skip to content
This repository has been archived by the owner on Feb 28, 2023. It is now read-only.

Commit

Permalink
fix: mapField does not handle array in schema array properly
Browse files Browse the repository at this point in the history
Co-authored-by: kevinjxx <[email protected]>
  • Loading branch information
logaretm and kevinjxx committed Aug 7, 2021
1 parent f106f6e commit 7daaf12
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 8 deletions.
23 changes: 15 additions & 8 deletions packages/plugin-vee-validate/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
74 changes: 74 additions & 0 deletions packages/plugin-vee-validate/tests/integration/integration.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: `
<SchemaWithValidation :schema="schema" />
`,
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('[email protected]')
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('')
})
})

0 comments on commit 7daaf12

Please sign in to comment.