Skip to content

Commit

Permalink
test: add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
electroluxcode committed Aug 29, 2024
1 parent f73e377 commit 2de57ad
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions packages/validator/src/__tests__/validator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,22 @@ import {
registerValidateFormats,
setValidateLanguage,
registerValidateMessageTemplateEngine,
registerMergeRules,
} from '../index'

registerValidateRules({
custom: (value) => (value === '123' ? 'custom error' : ''),
customBool: () => false,
customBool2: () => true,
customBool3: (value) => {
return value === 'registerValidateRules'
},
})

registerMergeRules({
customBool3: (value) => {
return value === 'registerMergeRules'
},
})

registerValidateFormats({
Expand Down Expand Up @@ -531,3 +541,39 @@ test('validator order with format', async () => {
'The field value is required'
)
})

test('validator merge', async () => {
registerMergeRules({
customBool3: (value) => {
return value === 'registerMergeRules'
},
})
noError(
await validate('registerMergeRules', {
customBool3: true,
message: 'custom error',
})
)
})

/**
* @description other test case should be before this one, because it will change the default rules
*/
test('validator merge required', async () => {
registerMergeRules({})
hasError(
await validate('', [{ required: true }]),
'The field value is required'
)

registerMergeRules({
required: () => {
return true
},
})
noError(
await validate('', {
required: true,
})
)
})

0 comments on commit 2de57ad

Please sign in to comment.