Skip to content

Commit

Permalink
feat: add option to switch to full verification (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyaliao authored Nov 5, 2024
1 parent c143529 commit 0e876fa
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
31 changes: 31 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,37 @@ describe('useForm', () => {
expect(wrapper.rule.age.validate('abc', { fullResult: true })).toStrictEqual({ valid: false, message: 'expect numbers' })
expect(wrapper.rule.age.validate('abc', { fullResult: false })).toEqual(false)
})

it('should verify all fields when fullValidation is true', async () => {
const wrapper = useSetup(() => {
const { form, status, verify } = useForm({
form: () => ({
name: '',
age: '',
email: '',
}),
rule: {
name: val => !!val || 'name required',
age: val => !!val || 'age required',
email: val => !!val || 'email required',
},
fullValidation: true,
})
return { form, status, verify }
})

wrapper.verify()
await nextTick()

expect(wrapper.status.name.isError).true
expect(wrapper.status.age.isError).true
expect(wrapper.status.email.isError).true
expect(wrapper.status.name.message).toBe('name required')
expect(wrapper.status.age.message).toBe('age required')
expect(wrapper.status.email.message).toBe('email required')

wrapper.unmount()
})
})

describe('object type field', () => {
Expand Down
22 changes: 16 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import { useDirtyFields, useIsError, useIsFormDirty } from './getters'
import { createSubmitter } from './submitter'
import { initRule } from './rule'

const defaultParam: Required<{ defaultMessage: UseFormDefaultMessage, lazy: UseFormLazy }> = {
const defaultParam: Required<{ defaultMessage: UseFormDefaultMessage, lazy: UseFormLazy, fullValidation: boolean }> = {
defaultMessage: '',
lazy: false,
fullValidation: false,
}

/**
Expand All @@ -21,7 +22,7 @@ const defaultParam: Required<{ defaultMessage: UseFormDefaultMessage, lazy: UseF
*/
export function useForm<FormT extends {}>(param: UseFormParam<FormT>): UseFormReturn<FormT> {
const options = Object.assign({}, defaultParam, param)
const { form: formBuilder, rule: formRule, defaultMessage: formDefaultMessage, lazy: formLazy } = options
const { form: formBuilder, rule: formRule, defaultMessage: formDefaultMessage, lazy: formLazy, fullValidation } = options

const initialForm = ref(formBuilder()) as Ref<FormT>
const form = reactive<FormT>(formBuilder())
Expand All @@ -39,7 +40,7 @@ export function useForm<FormT extends {}>(param: UseFormParam<FormT>): UseFormRe
dirtyFields,
isDirty: useIsFormDirty(dirtyFields),
isError: useIsError(status),
...createControl(formBuilder, initialForm, form, status),
...createControl(formBuilder, initialForm, form, status, fullValidation),
}

return {
Expand All @@ -54,12 +55,21 @@ function createControl<FormT extends {}>(
initialForm: Ref<FormT>,
form: FormT | UnwrapNestedRefs<FormT>,
status: Record<PropertyKey, StatusItem>,
fullValidation: boolean,
) {
const verify = () => {
let isPass = true
Object.keys(status).forEach((key) => {
isPass = isPass && status[key].verify()
})
if (fullValidation) {
Object.keys(status).forEach((key) => {
const fieldResult = status[key].verify()
isPass = isPass && fieldResult
})
}
else {
Object.keys(status).forEach((key) => {
isPass = isPass && status[key].verify()
})
}
return isPass
}

Expand Down
6 changes: 6 additions & 0 deletions src/type/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ export interface UseFormParam<FormT> {
* @default false
*/
lazy?: UseFormLazy
/**
* If `true`, all rules will be fully validated.
*
* @default false
*/
fullValidation?: boolean
}

export interface ValidateOptions<FullResult extends boolean> { fullResult?: FullResult }
Expand Down

0 comments on commit 0e876fa

Please sign in to comment.