Skip to content

Commit

Permalink
feat(useAsyncValidator): add manual option (#2903)
Browse files Browse the repository at this point in the history
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
  • Loading branch information
jaw52 and antfu committed Mar 28, 2023
1 parent 2e297db commit 16461db
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 7 deletions.
104 changes: 103 additions & 1 deletion packages/integrations/useAsyncValidator/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Rules } from 'async-validator'
import type { Ref } from 'vue-demi'
import { ref } from 'vue-demi'
import { nextTick, ref } from 'vue-demi'
import { useAsyncValidator } from '.'

describe('useAsyncValidator', () => {
Expand Down Expand Up @@ -211,5 +211,107 @@ describe('useAsyncValidator', () => {
`)

form.value.name = 'okxiaoliang4'
await nextTick()
expect(pass.value).toBe(true)
expect(errors.value).toMatchObject([])
})
})

describe('set manual true', () => {
let form: {
name: string
age: number
}

beforeEach(() => {
form = {
name: 'jelf',
age: 24,
}
})

it('set immediate and manual at the same time', async () => {
const rules: Rules = {
name: {
type: 'string',
min: 5,
max: 20,
message: 'name length must be 5-20',
},
age: {
type: 'number',
},
}
const { pass, errors, then } = useAsyncValidator(form, rules, { immediate: false, manual: true })
expect(pass.value).toBe(true)
expect(errors.value).toMatchObject([])
then(() => {
expect(pass.value).toBe(true)
expect(errors.value).toMatchObject([])
})
})

it('set manual, do not run validator automatically', async () => {
const rules: Rules = {
name: {
type: 'string',
min: 5,
max: 20,
message: 'name length must be 5-20',
},
age: {
type: 'number',
},
}
const { pass, errors, then } = useAsyncValidator(form, rules, { manual: true })
expect(pass.value).toBe(true)
expect(errors.value).toMatchObject([])
then(() => {
expect(pass.value).toBe(true)
expect(errors.value).toMatchObject([])
})
})

it('manual trigger validator', async () => {
const form = ref({
name: 'jelf',
age: 24,
})

const rules = ref({
name: {
type: 'string',
min: 5,
max: 20,
message: 'name length must be 5-20',
},
age: {
type: 'number',
},
}) as Ref<Rules>

const { execute, pass, errors } = useAsyncValidator(form, rules, { manual: true })

expect(pass.value).toBe(true)
expect(errors.value).toMatchObject([])

// first trigger
await execute()
expect(pass.value).toBe(false)
expect(errors.value).toMatchInlineSnapshot(`
[
{
"field": "name",
"fieldValue": "jelf",
"message": "name length must be 5-20",
},
]
`)

// second trigger
form.value.name = 'okxiaoliang4'
await execute()
expect(pass.value).toBe(true)
expect(errors.value).toMatchObject([])
})
})
25 changes: 19 additions & 6 deletions packages/integrations/useAsyncValidator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,17 @@ export interface UseAsyncValidatorOptions {
* @see https://github.com/yiminghe/async-validator#options
*/
validateOption?: ValidateOption
/**
* The validation will be triggered right away for the first time.
* Only works when `manual` is not set to true.
*
* @default true
*/
immediate?: boolean
/**
* If set to true, the validation will not be triggered automatically.
*/
manual?: boolean
}

/**
Expand All @@ -51,13 +61,14 @@ export function useAsyncValidator(
const {
validateOption = {},
immediate = true,
manual = false,
} = options

const valueRef = resolveRef(value)

const errorInfo = shallowRef<AsyncValidatorError | null>(null)
const isFinished = ref(true)
const pass = ref(!immediate)
const pass = ref(!immediate || manual)
const errors = computed(() => errorInfo.value?.errors || [])
const errorFields = computed(() => errorInfo.value?.fields || {})

Expand Down Expand Up @@ -87,11 +98,13 @@ export function useAsyncValidator(
}
}

watch(
[valueRef, validator],
() => execute(),
{ immediate, deep: true },
)
if (!manual) {
watch(
[valueRef, validator],
() => execute(),
{ immediate, deep: true },
)
}

const shell = {
isFinished,
Expand Down

0 comments on commit 16461db

Please sign in to comment.