Skip to content

Commit

Permalink
fix: 修复校验函数类型错误 (#2949)
Browse files Browse the repository at this point in the history
Co-authored-by: xiamiao <[email protected]>
  • Loading branch information
xiamiao1121 and xiamiao authored Jul 25, 2024
1 parent 3d070cd commit ddcb0ae
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions packages/ui/form/src/use-form-field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ import { useEffect, useCallback, useMemo } from 'react'
import { FormFieldPath, FormRuleModel, FormRuleType } from './types'
import { useFormContext } from './context'
import { isArrayNonEmpty } from '@hi-ui/type-assertion'
import Validater, { Rules } from 'async-validator'
import Validater, {
InternalRuleItem,
Rules,
RuleItem,
ValidateOption,
Values as ValidateValues,
Value,
} from 'async-validator'
import { normalizeArray } from '@hi-ui/array-utils'
import { isValidField, stringify } from './utils'

Expand All @@ -27,7 +34,7 @@ export const useFormField = <Values = any>(props: UseFormFieldProps<Values>) =>

const { getFieldProps, registerField, unregisterField } = useFormContext()

const fieldRules: Rules[] = useFiledRules(props)
const fieldRules: RuleItem[] = useFiledRules(props)

// 当前 field 的唯一校验器
const fieldValidate = useCallback(
Expand All @@ -44,10 +51,22 @@ export const useFormField = <Values = any>(props: UseFormFieldProps<Values>) =>
if (rule.validator) {
return {
...rule,
validator: (validatorRule: any, value: any, cb: any) => {
validator: (
validatorRule: any,
value: Value,
callback: (error?: string | Error) => void,
source: ValidateValues,
options: ValidateOption
) => {
const field = validatorRule.field.replace(/"/g, '')
const fullField = validatorRule.fullField.replace(/"/g, '')
rule.validator({ ...validatorRule, field, fullField }, value, cb)
rule.validator!(
{ ...validatorRule, field, fullField } as InternalRuleItem,
value,
callback,
source,
options
)
},
}
} else
Expand Down

0 comments on commit ddcb0ae

Please sign in to comment.