From 7f977709ecaafd7c9f2a2297c83c1dceb063dd2c Mon Sep 17 00:00:00 2001 From: okxiaoliang4 <353742991@qq.com> Date: Wed, 15 Jun 2022 18:26:34 +0800 Subject: [PATCH] feat(useAsyncValidator): new options --- .../integrations/useAsyncValidator/index.ts | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/packages/integrations/useAsyncValidator/index.ts b/packages/integrations/useAsyncValidator/index.ts index 5f89111dd65..26fcd097008 100644 --- a/packages/integrations/useAsyncValidator/index.ts +++ b/packages/integrations/useAsyncValidator/index.ts @@ -1,16 +1,16 @@ import type { MaybeRef } from '@vueuse/shared' import { until } from '@vueuse/shared' import Schema from 'async-validator' -import type { Rules, ValidateError } from 'async-validator' +import type { Rules, ValidateError, ValidateOption } from 'async-validator' import type { Ref } from 'vue-demi' import { computed, ref, unref, watchEffect } from 'vue-demi' -type AsyncValidatorError = Error & { +export type AsyncValidatorError = Error & { errors: ValidateError[] fields: Record } -interface UseAsyncValidatorReturn { +export interface UseAsyncValidatorReturn { pass: Ref errorInfo: Ref isFinished: Ref @@ -18,24 +18,34 @@ interface UseAsyncValidatorReturn { errorFields: Ref } +export interface UseAsyncValidatorOptions { + /** + * @see https://github.com/yiminghe/async-validator#options + */ + validateOption?: ValidateOption +} + /** * Wrapper for async-validator. * * @see https://vueuse.org/useAsyncValidator + * @see https://github.com/yiminghe/async-validator */ -export function useAsyncValidator(value: MaybeRef>, rules: MaybeRef): UseAsyncValidatorReturn & PromiseLike { +export function useAsyncValidator(value: MaybeRef>, rules: MaybeRef, options: UseAsyncValidatorOptions = {}): UseAsyncValidatorReturn & PromiseLike { const errorInfo = ref() const isFinished = ref(false) const pass = ref(false) const errors = computed(() => errorInfo.value?.errors || []) const errorFields = computed(() => errorInfo.value?.fields || {}) + const { validateOption = {} } = options + watchEffect(async () => { isFinished.value = false pass.value = false const validator = new Schema(unref(rules)) try { - await validator.validate(unref(value)) + await validator.validate(unref(value), validateOption) pass.value = true errorInfo.value = null }