Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Form)!: resolve async validation in yup & issue directly mutate state #2702

Merged
merged 3 commits into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/runtime/components/Form.vue
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ function resolveErrorIds(errs: FormError[]): FormErrorWithId[] {
}))
}

const parsedValue = ref<T | null>(null)
async function getErrors(): Promise<FormErrorWithId[]> {
let errs = props.validate ? (await props.validate(props.state)) ?? [] : []

Expand All @@ -112,7 +113,7 @@ async function getErrors(): Promise<FormErrorWithId[]> {
if (errors) {
errs = errs.concat(errors)
} else {
Object.assign(props.state, result)
parsedValue.value = result
}
}

Expand Down Expand Up @@ -169,7 +170,7 @@ async function onSubmitWrapper(payload: Event) {

try {
await _validate({ nested: true })
event.data = props.state
event.data = props.schema ? parsedValue.value : props.state
await props.onSubmit?.(event)
} catch (error) {
if (!(error instanceof FormValidationException)) {
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/utils/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ async function validateYupSchema(
schema: YupObjectSchema<any>
): Promise<ValidateReturnSchema<typeof state>> {
try {
const result = schema.validateSync(state, { abortEarly: false })
const result = await schema.validate(state, { abortEarly: false })
return {
errors: null,
result
Expand Down
2 changes: 0 additions & 2 deletions test/components/Form.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,15 +422,13 @@ describe('Form', () => {
`
})
const form = wrapper.setupState.form
const state = wrapper.setupState.state

const inputEl = wrapper.find('#input')
inputEl.setValue(input.value)

form.value.submit()
await flushPromises()

expect(state.value).toEqual(expected.value)
expect(wrapper.setupState.onSubmit).toHaveBeenCalledWith(expect.objectContaining({
data: expected
}))
Expand Down
Loading