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

feat: add option to switch to full verification #54

Merged
merged 3 commits into from
Nov 5, 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
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
Loading