Skip to content

Commit

Permalink
test: add message scope (#3886)
Browse files Browse the repository at this point in the history
* test: add format and scope

* test: add message scope
  • Loading branch information
hchlq authored Jul 4, 2023
1 parent 287fdad commit 0ac09f9
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions packages/validator/src/__tests__/validator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,28 @@ test('multi validate', async () => {
})
})

test('message scope', async () => {
const results = await validate(
'',
{
required: true,
validator() {
return 'validate error {{name}}'
},
},
{
context: {
name: 'scopeName',
},
}
)
expect(results).toEqual({
error: ['The field value is required', 'validate error scopeName'],
success: [],
warning: [],
})
})

test('first validate', async () => {
const results = await validate(
'',
Expand Down Expand Up @@ -469,6 +491,29 @@ test('validator template with format', async () => {
)
})

test('validator template with format and scope', async () => {
registerValidateMessageTemplateEngine((message) => {
if (typeof message !== 'string') return message
return message.replace(/\<\<\s*([\w.]+)\s*\>\>/g, (_, $0) => {
return { aa: 123 }[$0]
})
})

const result = await validate(
'',
(value, rules, ctx, format) => {
return `<<aa>>=123&${format('<<aa>>{{name}}')}`
},
{
context: {
name: 'scopeName',
},
}
)

expect(result.error[0]).toEqual('123=123&123scopeName')
})

test('validator order with format', async () => {
hasError(
await validate('', [
Expand Down

0 comments on commit 0ac09f9

Please sign in to comment.