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(rule): support for using the values of other fields in the form in rule functions #46

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
49 changes: 49 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -564,4 +564,53 @@ describe('object type field', () => {
expect(wr.status.obj.isError).true
expect(wr.status.obj.message).toBe('need truthy')
})

// rule callback 可以通过 full 参数获取表单其它的值
it('rule callback full params', async () => {
const wrapper = useSetup(() => {
const { form, status, isError } = useForm({
form: () => ({
password1: '',
password2: '',
}),
rule: {
password1: val => !!val || 'required',
password2: (val, full) => full.password1 === val || 'no equal',
},
})
return { form, status, isError }
})

await nextTick()
// age
expect(wrapper.status.password1.isError).false
expect(wrapper.status.password1.message).toBe('')
// name
expect(wrapper.status.password2.isError).false
expect(wrapper.status.password2.message).toBe('')
// any error
expect(wrapper.isError).false

wrapper.form.password1 = '123456'
await nextTick()
expect(wrapper.status.password2.isError).false
expect(wrapper.status.password2.message).toBe('')

wrapper.form.password1 = ''
await nextTick()
expect(wrapper.status.password2.isError).false
expect(wrapper.status.password2.message).toBe('')

wrapper.form.password2 = '654321'
await nextTick()
expect(wrapper.status.password2.isError).true
expect(wrapper.status.password2.message).toBe('no equal')

wrapper.form.password1 = '654321'
await nextTick()
expect(wrapper.status.password2.isError).false
expect(wrapper.status.password2.message).toBe('')

wrapper.unmount()
})
})
10 changes: 5 additions & 5 deletions src/type/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ import type { ComputedRef, DeepReadonly, UnwrapNestedRefs } from 'vue'
import type { FormStatus } from './formStatus'
import type { Submitter } from './submitter'

export type RuleItem<ValueT = any> = ((val: ValueT) => boolean | string)
export type RuleItem<ValueT = unknown, FormT = unknown> = ((val: ValueT, full: DeepReadonly<FormT>) => boolean | string)

export type UseFormBuilder<Form extends {} = {}> = () => Form
export type UseFormRule<FormT extends {}> = {
readonly [K in keyof FormT]?: RuleItem<FormT[K]> | RuleItem<FormT[K]>[]
export type UseFormBuilder<Form extends object> = () => Form
export type UseFormRule<FormT> = {
readonly [K in keyof FormT]?: RuleItem<FormT[K], FormT> | RuleItem<FormT[K], FormT>[]
}
export type UseFormDefaultMessage = string
export type UseFormLazy = boolean

export interface UseFormParam<FormT> {
export interface UseFormParam<FormT extends object> {
/** Initial form value */
form: UseFormBuilder<FormT>
/** Verification rules */
Expand Down
2 changes: 1 addition & 1 deletion src/type/formStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ export interface StatusItem {
_ignoreUpdate: IgnoredUpdater
}

export type FormStatus<FormT extends {}> = {
export type FormStatus<FormT> = {
readonly [K in keyof FormT]: StatusItem
}
4 changes: 2 additions & 2 deletions src/type/submitter.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { Ref } from 'vue'
import type { UseFormReturn } from './form'

export type SubmitFunction<FormT = any, FnReturnT = any> = (formData: Omit<UseFormReturn<FormT>, 'submitter'>) => FnReturnT
export type SubmitFunction<FormT extends object = any, FnReturnT = any> = (formData: Omit<UseFormReturn<FormT>, 'submitter'>) => FnReturnT

export type Submitter<FormT extends {}> = <FnT extends SubmitFunction<FormT>>(
export type Submitter<FormT extends object> = <FnT extends SubmitFunction<FormT>>(
fn: FnT,
options?: CreateSubmitOptions,
) => CreateSubmitReturn<FnT>
Expand Down