Skip to content

Commit

Permalink
fix: 解决required等属性的false不生效问题 && 支持message和validator等字符串式的函数
Browse files Browse the repository at this point in the history
  • Loading branch information
electroluxcode committed Sep 5, 2024
1 parent 9ab450f commit 9ad3dde
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 4 deletions.
1 change: 1 addition & 0 deletions packages/json-schema/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './schema'
export * from './types'
export * from './compiler'
1 change: 1 addition & 0 deletions packages/shared/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export * from './compare'
export * from './checkers'
export * from './clone'
export * from './isEmpty'
export * from './isFalse'
export * from './case'
export * from './string'
export * from './global'
Expand Down
6 changes: 6 additions & 0 deletions packages/shared/src/isFalse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* @description 判断 value 是否为 false
*/
export const isFalse = (value: any) => {
return value === false
}
25 changes: 21 additions & 4 deletions packages/validator/src/parser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { isArr, isBool, isFn, isStr } from '@formily/shared'
import { isArr, isBool, isFn, isStr, isFalse } from '@formily/shared'
import { shallowCompile } from '@formily/json-schema'
import {
ValidatorDescription,
ValidatorFunction,
Expand All @@ -12,6 +13,14 @@ import { getValidateRules, getValidateLocale } from './registry'
import { render } from './template'

const getRuleMessage = (rule: IValidatorRules, type: string) => {
if (isFn(rule.message)) {
return rule.message
}
// message 支持字符串形式的函数
if (isStr(rule.message)) {
let message = rule.message ? shallowCompile(rule.message) : rule.message
return message
}
if (rule.format) {
return rule.message || getValidateLocale(rule.format)
}
Expand Down Expand Up @@ -42,18 +51,23 @@ export const parseValidatorDescriptions = <Context = any>(
return parseValidatorDescription(description)
})
}
let shouldJuadgeBoolKey = ['whitespace', 'uniqueItems']

export const parseValidatorRules = (
rules: IValidatorRules = {}
): ValidatorParsedFunction[] => {
const getRulesKeys = (): string[] => {
const keys = []
if ('required' in rules) {
if ('required' in rules && !isFalse(rules.required)) {
keys.push('required')
}
for (let key in rules) {
if (key === 'required' || key === 'validator') continue
keys.push(key)
if (shouldJuadgeBoolKey.includes(key) && !isFalse(key)) {
keys.push(key)
} else {
keys.push(key)
}
}
if ('validator' in rules) {
keys.push('validator')
Expand All @@ -68,8 +82,11 @@ export const parseValidatorRules = (
}
}
const createValidate =
(callback: ValidatorFunction, message: string) =>
(callback: ValidatorFunction, message: string | Function) =>
async (value: any, context: any) => {
if (isFn(message)) {
message = message(value, context) as string
}
const context_ = getContext(context, value)
try {
const results = await callback(
Expand Down
2 changes: 2 additions & 0 deletions packages/validator/src/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
IRegistryLocales,
IRegistryRules,
} from './types'
import { shallowCompile } from '@formily/json-schema'

const getIn = FormPath.getIn

Expand Down Expand Up @@ -101,6 +102,7 @@ export const registerValidateLocale = (locale: IRegistryLocales) => {

export const registerValidateRules = (rules: IRegistryRules) => {
each(rules, (rule, key) => {
rule = shallowCompile(rule)
if (isFn(rule)) {
registry.rules[key] = rule
}
Expand Down

0 comments on commit 9ad3dde

Please sign in to comment.