Skip to content

Commit 8f02e71

Browse files
committed
fix(schema): 优化SchemaType接口的类型定义和默认消息处理逻辑
1 parent 09a1cba commit 8f02e71

File tree

1 file changed

+13
-23
lines changed

1 file changed

+13
-23
lines changed

packages/utils/src/validate/schema.ts

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,22 @@ import { hasOwn, isFunction } from '../type'
1717
interface SchemaType {
1818
rules: Record<string, any> | null
1919
_messages: Record<string, any>
20-
define(descriptor: Record<string, any>): void
21-
messages(messages?: Record<string, any>): Record<string, any>
22-
getSeries(options: any, source: any, source_: any): Record<string, any>
23-
mergeMessage(options: any): void
24-
validate(source_: any, o?: any, oc?: Function): Promise<any>
25-
getValidationMethod(rule: any): any
26-
getType(rule: any): string
20+
define: (descriptor: Record<string, any>) => void
21+
messages: (messages?: Record<string, any>) => Record<string, any>
22+
getSeries: (options: any, source: any, source_: any) => Record<string, any>
23+
mergeMessage: (options: any) => void
24+
validate: (source_: any, o?: any, oc?: Function) => Promise<any>
25+
getValidationMethod: (rule: any) => any
26+
getType: (rule: any) => string
2727
}
2828

29-
function Schema(this: SchemaType, descriptor: Record<string, any>, translate?: any) {
30-
Schema.getDefaultMessage = (trans?: any) => {
31-
// 返回一个默认消息对象而不是undefined
32-
return trans ? { ...trans } : ({} as Record<string, any>)
33-
}
34-
35-
Schema.getSystemMessage = (trans?: any) => {
36-
return Schema.getDefaultMessage(trans)
37-
}
38-
29+
function Schema(descriptor: Record<string, any>, translate?: any) {
30+
Schema.getSystemMessage = () => Schema.getDefaultMessage(translate)
3931
Schema.messages = Schema.getSystemMessage(translate)
4032
Schema.systemMessages = Schema.messages
41-
42-
this.rules = null
43-
this._messages = Schema.systemMessages
44-
this.define(descriptor)
33+
;(this as SchemaType).rules = null
34+
;(this as SchemaType)._messages = Schema.systemMessages
35+
;(this as SchemaType).define(descriptor)
4536
}
4637

4738
/**
@@ -225,8 +216,7 @@ const asyncCallback =
225216
Schema.prototype = {
226217
messages(messages) {
227218
if (messages) {
228-
const systemMessages = Schema.getSystemMessage() || {}
229-
this._messages = deepMerge(systemMessages, messages)
219+
this._messages = deepMerge(Schema.getSystemMessage(), messages)
230220
}
231221

232222
return this._messages

0 commit comments

Comments
 (0)