react-component / react-component/field-form

什么时候删除自定义校验函数的包装器?

Open
#332 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
1k
Forks
286
Avg merge
4d 16h
Merged PRs (30d)
1

Description

validateRules 函数会对自定义校验函数进行包装,这导致无法全面接收由 async-validator 传递过来的参数,比如 sourceoptions

全面接收参数的好处

  1. 验证出错时,通过响应 options.messages.[customRule] 消息即可消费 messageVariables
  2. 对于通过使用 ConfigProvider 做全局表单 validateMessages 提供了一致的地区语言方案
  3. 更简洁的代码

目前的解决方案

通过使用 asyncValidator 属性来避开包装器

import type { FormInstance, NamePath, RuleRender } from 'rc-field-form/lib/interface';

type ValidateMessages = import('rc-field-form/lib/interface').ValidateMessages & {
  confirmed: string;
};

type RuleObject = import('rc-field-form/lib/interface').RuleObject & {
  asyncValidator: (
    rule: RuleObject,
    value: import('rc-field-form/lib/interface').StoreValue,
    callback: (error?: string) => void,
    source: Record<string, import('rc-field-form/lib/interface').StoreValue>,
    options: import('rc-field-form/lib/interface').ValidateOptions & { messages: ValidateMessages }
  ) => void | Promise<void | string>;
}

export function confirmed(name: NamePath): RuleRender {
  return ({ getFieldValue }: FormInstance): RuleObject => ({
    asyncValidator: (rule, value, done, source, options) =>
      done(!value || getFieldValue(name) === value ? undefined : options.messages.confirmed),
  });
}

使用示例

自定义验证器示例

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in src/utils/validateUtil.ts at validateRules, then compare its custom-validator wrapping with the async-validator v3.0.3 callback signature. Review the linked custom validation example and verify that the eventual behavior exposes source and options while preserving validateMessages and messageVariables handling.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
frontend
Issue type
Refactor
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.