From 545e0206b648262c6233b22afd0f4afb66df8315 Mon Sep 17 00:00:00 2001 From: electroluxcode <3451613934@qq.com> Date: Thu, 5 Sep 2024 23:30:09 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=A7=A3=E5=86=B3=E5=85=B6=E4=BB=96?= =?UTF-8?q?=E5=B1=9E=E6=80=A7=E7=9A=84false=E4=B8=8D=E7=94=9F=E6=95=88?= =?UTF-8?q?=E9=97=AE=E9=A2=98=20&&=20=E6=B7=BB=E5=8A=A0xvalidator=E5=85=88?= =?UTF-8?q?=E5=90=8E=E8=A7=84=E5=88=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/json-schema/src/shared.ts | 18 +++++++++++++++--- packages/validator/src/parser.ts | 5 +---- packages/validator/src/registry.ts | 25 +++++++++++++++++++++---- 3 files changed, 37 insertions(+), 11 deletions(-) diff --git a/packages/json-schema/src/shared.ts b/packages/json-schema/src/shared.ts index 57927a550cd..093e566373d 100644 --- a/packages/json-schema/src/shared.ts +++ b/packages/json-schema/src/shared.ts @@ -1,8 +1,16 @@ -import { isFn, each, isPlainObj, isArr, toArr, FormPath } from '@formily/shared' +import { + isFn, + each, + isPlainObj, + isArr, + toArr, + FormPath, + isObj, +} from '@formily/shared' import { isObservable, untracked } from '@formily/reactive' import { Schema } from './schema' import { ISchema } from './types' -import { registerValidateRules } from '@formily/validator' +import { registerMergeRules } from '@formily/validator' const REVA_ACTIONS_KEY = Symbol.for('__REVA_ACTIONS') @@ -107,7 +115,11 @@ export const traverseSchema = ( ['x-validator'], schema['x-compile-omitted']?.includes('x-validator') ) - registerValidateRules(schema['x-validator']) + // 避免 Array 类型的 x-validator 被注册 + // 多key校验规则才注册到rules中,否则不注册 + if (isObj(schema['x-validator']) && !schema['x-validator']['message']) { + registerMergeRules(schema['x-validator'] as Record) + } } const seenObjects = [] const root = schema diff --git a/packages/validator/src/parser.ts b/packages/validator/src/parser.ts index 205b4111aab..9d4b7efe3dc 100644 --- a/packages/validator/src/parser.ts +++ b/packages/validator/src/parser.ts @@ -51,7 +51,6 @@ export const parseValidatorDescriptions = ( return parseValidatorDescription(description) }) } -let shouldJuadgeBoolKey = ['whitespace', 'uniqueItems'] export const parseValidatorRules = ( rules: IValidatorRules = {} @@ -63,9 +62,7 @@ export const parseValidatorRules = ( } for (let key in rules) { if (key === 'required' || key === 'validator') continue - if (shouldJuadgeBoolKey.includes(key) && !isFalse(key)) { - keys.push(key) - } else { + if (!isFalse(rules[key])) { keys.push(key) } } diff --git a/packages/validator/src/registry.ts b/packages/validator/src/registry.ts index 02988f3705c..8be5e9008ac 100644 --- a/packages/validator/src/registry.ts +++ b/packages/validator/src/registry.ts @@ -39,7 +39,10 @@ const registry = { language: getBrowserlanguage(), }, formats: {}, - rules: {}, + rules: { + global: {}, + merge: {}, + }, template: null, } @@ -93,8 +96,13 @@ export const getValidateRules = ( key?: T ): T extends string ? ValidatorFunction - : { [key: string]: ValidatorFunction } => - key ? registry.rules[key as any] : registry.rules + : { [key: string]: ValidatorFunction } => { + let rules = { + ...registry.rules['global'], + ...registry.rules['merge'], + } + return key ? rules[key as any] : rules +} export const registerValidateLocale = (locale: IRegistryLocales) => { registry.locales.messages = deepmerge(registry.locales.messages, locale) @@ -104,7 +112,16 @@ export const registerValidateRules = (rules: IRegistryRules) => { each(rules, (rule, key) => { rule = shallowCompile(rule) if (isFn(rule)) { - registry.rules[key] = rule + registry.rules.global[key] = rule + } + }) +} + +export const registerMergeRules = (rules: IRegistryRules) => { + each(rules, (rule, key) => { + rule = shallowCompile(rule) + if (isFn(rule)) { + registry.rules.merge[key] = rule } }) }