Skip to content

Commit

Permalink
feat(EasyValidator): 验证结果包含未验证通过的属性的提示信息
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Jun 26, 2019
1 parent 8fc1cf7 commit 0e60b45
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/EasyValidator.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { EasyValidator, EasyValidatorRules } from './EasyValidator'
import { ii } from './ii'
import { jestExpectEqual } from './enhanceJest'
import { pluck } from './pluck'
import { wait } from './wait'

type Data = {
Expand Down Expand Up @@ -129,6 +131,7 @@ test('综合测试', async () => {
{
valid: true,
unvalidRules: [],
messages: {},
},
)
jestExpectEqual(
Expand All @@ -149,9 +152,8 @@ test('综合测试', async () => {
required: undefined as any, // 13
updateMessage: 'y', // 14
}),
{
valid: false,
unvalidRules: [
ii(() => {
const unvalidRules = [
rules[1],
rules[5],
rules[8],
Expand All @@ -162,8 +164,13 @@ test('综合测试', async () => {
...rules[14],
message: '出错啦',
},
],
},
]
return {
valid: false,
unvalidRules: unvalidRules,
messages: pluck(unvalidRules, item => item.message, item => item.key),
}
}),
)
})

Expand Down
4 changes: 4 additions & 0 deletions src/EasyValidator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { isChineseIDCardNumber, isEmail, isFunction, isInteger, isNumeric, isPossibleChineseMobilePhoneNumber, isPossibleChineseName, isPromiseLike, isRegExp, isUrl } from './is'
import { pluck } from './pluck'
import { sequential } from './sequential'

export type EasyValidatorData = Record<keyof any, any>
Expand Down Expand Up @@ -61,6 +62,8 @@ export interface EasyValidatorValidateReturn<D extends EasyValidatorData> {
valid: boolean,
/** 未验证通过的规则组成的列表 */
unvalidRules: Array<EasyValidatorRule<D>>,
/** 未验证通过的属性的提示信息 */
messages: Partial<Record<keyof D, any>>,
}

/**
Expand Down Expand Up @@ -235,6 +238,7 @@ export class EasyValidator<D extends EasyValidatorData> {
).then<EasyValidatorValidateReturn<D>>(() => ({
valid: unvalidRules.length === 0,
unvalidRules: unvalidRules.slice(),
messages: pluck(unvalidRules, item => item.message, item => item.key),
}))
)
}
Expand Down

0 comments on commit 0e60b45

Please sign in to comment.