Skip to content

Commit

Permalink
refactor(Form): rename validation functions and vars for clarity (#3029)
Browse files Browse the repository at this point in the history
Co-authored-by: Benjamin Canac <[email protected]>
  • Loading branch information
romhml and benjamincanac authored Jan 13, 2025
1 parent 9d0f861 commit 1a54ab2
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 13 deletions.
13 changes: 7 additions & 6 deletions src/runtime/components/Form.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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 { parseSchema } from '../utils/form'
import { validateSchema } from '../utils/form'
import { FormValidationException } from '../types/form'
const props = withDefaults(defineProps<FormProps<T>>(), {
Expand Down Expand Up @@ -104,16 +104,17 @@ function resolveErrorIds(errs: FormError[]): FormErrorWithId[] {
}))
}
const parsedValue = ref<T | null>(null)
const transformedState = ref<T | null>(null)
async function getErrors(): Promise<FormErrorWithId[]> {
let errs = props.validate ? (await props.validate(props.state)) ?? [] : []
if (props.schema) {
const { errors, result } = await parseSchema(props.state, props.schema as FormSchema<typeof props.state>)
const { errors, result } = await validateSchema(props.state, props.schema as FormSchema<typeof props.state>)
if (errors) {
errs = errs.concat(errors)
} else {
parsedValue.value = result
transformedState.value = result
}
}
Expand Down Expand Up @@ -170,7 +171,7 @@ async function onSubmitWrapper(payload: Event) {
try {
await _validate({ nested: true })
event.data = props.schema ? parsedValue.value : props.state
event.data = props.schema ? transformedState.value : props.state
await props.onSubmit?.(event)
} catch (error) {
if (!(error instanceof FormValidationException)) {
Expand All @@ -180,7 +181,7 @@ async function onSubmitWrapper(payload: Event) {
const errorEvent: FormErrorEvent = {
...event,
errors: error.errors,
childrens: error.childrens
children: error.children
}
emits('error', errorEvent)
}
Expand Down
6 changes: 3 additions & 3 deletions src/runtime/types/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export type FormSubmitEvent<T> = SubmitEvent & { data: T }

export type FormValidationError = {
errors: FormErrorWithId[]
childrens: FormValidationError[]
children?: FormValidationError[]
}

export type FormErrorEvent = SubmitEvent & FormValidationError
Expand Down Expand Up @@ -93,13 +93,13 @@ export interface ValidateReturnSchema<T> {
export class FormValidationException extends Error {
formId: string | number
errors: FormErrorWithId[]
childrens: FormValidationException[]
children?: FormValidationException[]

constructor(formId: string | number, errors: FormErrorWithId[], childErrors: FormValidationException[]) {
super('Form validation exception')
this.formId = formId
this.errors = errors
this.childrens = childErrors
this.children = childErrors
Object.setPrototypeOf(this, FormValidationException.prototype)
}
}
6 changes: 3 additions & 3 deletions src/runtime/utils/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export function isStandardSchema(schema: any): schema is StandardSchemaV1 {
return '~standard' in schema
}

export async function validateStandarSchema(
export async function validateStandardSchema(
state: any,
schema: StandardSchemaV1
): Promise<ValidateReturnSchema<typeof state>> {
Expand Down Expand Up @@ -192,7 +192,7 @@ async function validateValibotSchema(
}
}

export function parseSchema<T extends object>(state: T, schema: FormSchema<T>): Promise<ValidateReturnSchema<typeof state>> {
export function validateSchema<T extends object>(state: T, schema: FormSchema<T>): Promise<ValidateReturnSchema<typeof state>> {
if (isZodSchema(schema)) {
return validateZodSchema(state, schema)
} else if (isJoiSchema(schema)) {
Expand All @@ -204,7 +204,7 @@ export function parseSchema<T extends object>(state: T, schema: FormSchema<T>):
} else if (isSuperStructSchema(schema)) {
return validateSuperstructSchema(state, schema)
} else if (isStandardSchema(schema)) {
return validateStandarSchema(state, schema)
return validateStandardSchema(state, schema)
} else {
throw new Error('Form validation failed: Unsupported form schema')
}
Expand Down
2 changes: 1 addition & 1 deletion test/components/Form.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ describe('Form', () => {
expect(wrapper.setupState.onSubmit).not.toHaveBeenCalled()
expect(wrapper.setupState.onError).toHaveBeenCalledTimes(1)
const onErrorCallArgs = wrapper.setupState.onError.mock.lastCall[0]
expect(onErrorCallArgs.childrens[0].errors).toMatchObject([{ id: 'nestedInput', name: 'field', message: 'Required' }])
expect(onErrorCallArgs.children[0].errors).toMatchObject([{ id: 'nestedInput', name: 'field', message: 'Required' }])
expect(onErrorCallArgs.errors).toMatchObject([
{ id: 'emailInput', name: 'email', message: 'Required' },
{ id: 'passwordInput', name: 'password', message: 'Required' }
Expand Down

0 comments on commit 1a54ab2

Please sign in to comment.