feat(Form): apply transformations (#2550)

Co-authored-by: Romain Hamel <rom.hml@gmail.com>
This commit is contained in:
kyyy
2024-11-12 22:43:40 +07:00
committed by GitHub
parent 95aa6f68b3
commit 75c5e87724
4 changed files with 240 additions and 82 deletions

View File

@@ -38,7 +38,7 @@ extendDevtoolsMeta({ example: 'FormExample' })
import { provide, inject, nextTick, ref, onUnmounted, onMounted, computed, useId, readonly } from 'vue'
import { useEventBus } from '@vueuse/core'
import { formOptionsInjectionKey, formInputsInjectionKey, formBusInjectionKey, formLoadingInjectionKey } from '../composables/useFormField'
import { getYupErrors, isYupSchema, getValibotErrors, isValibotSchema, getZodErrors, isZodSchema, getJoiErrors, isJoiSchema, getStandardErrors, isStandardSchema, getSuperStructErrors, isSuperStructSchema } from '../utils/form'
import { parseSchema } from '../utils/form'
import { FormValidationException } from '../types/form'
const props = withDefaults(defineProps<FormProps<T>>(), {
@@ -108,20 +108,11 @@ async function getErrors(): Promise<FormErrorWithId[]> {
let errs = props.validate ? (await props.validate(props.state)) ?? [] : []
if (props.schema) {
if (isZodSchema(props.schema)) {
errs = errs.concat(await getZodErrors(props.state, props.schema))
} else if (isYupSchema(props.schema)) {
errs = errs.concat(await getYupErrors(props.state, props.schema))
} else if (isJoiSchema(props.schema)) {
errs = errs.concat(await getJoiErrors(props.state, props.schema))
} else if (isValibotSchema(props.schema)) {
errs = errs.concat(await getValibotErrors(props.state, props.schema))
} else if (isSuperStructSchema(props.schema)) {
errs = errs.concat(await getSuperStructErrors(props.state, props.schema))
} else if (isStandardSchema(props.schema)) {
errs = errs.concat(await getStandardErrors(props.state, props.schema))
const { errors, result } = await parseSchema(props.state, props.schema as FormSchema<typeof props.state>)
if (errors) {
errs = errs.concat(errors)
} else {
throw new Error('Form validation failed: Unsupported form schema')
Object.assign(props.state, result)
}
}