Skip to content

Commit

Permalink
fix: 解决其他属性的false不生效问题 && 添加xvalidator先后规则
Browse files Browse the repository at this point in the history
  • Loading branch information
electroluxcode committed Sep 5, 2024
1 parent 9ad3dde commit 545e020
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
18 changes: 15 additions & 3 deletions packages/json-schema/src/shared.ts
Original file line number Diff line number Diff line change
@@ -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')

Expand Down Expand Up @@ -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<string, any>)
}
}
const seenObjects = []
const root = schema
Expand Down
5 changes: 1 addition & 4 deletions packages/validator/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ export const parseValidatorDescriptions = <Context = any>(
return parseValidatorDescription(description)
})
}
let shouldJuadgeBoolKey = ['whitespace', 'uniqueItems']

export const parseValidatorRules = (
rules: IValidatorRules = {}
Expand All @@ -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)
}
}
Expand Down
25 changes: 21 additions & 4 deletions packages/validator/src/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ const registry = {
language: getBrowserlanguage(),
},
formats: {},
rules: {},
rules: {
global: {},
merge: {},
},
template: null,
}

Expand Down Expand Up @@ -93,8 +96,13 @@ export const getValidateRules = <T>(
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)
Expand All @@ -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
}
})
}
Expand Down

0 comments on commit 545e020

Please sign in to comment.