Skip to content

Commit

Permalink
fix(core): fix validator will trigger multi times with duplicate trig…
Browse files Browse the repository at this point in the history
…gerTypes (#2495)
  • Loading branch information
NexxLuo authored Nov 24, 2021
1 parent ad48597 commit 88d6f83
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
18 changes: 18 additions & 0 deletions packages/core/src/__tests__/field.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1716,3 +1716,21 @@ test('nested field modified', async () => {
await form.reset()
expect(form.modified).toBeFalsy()
})

test('field setValidator repeat call', async () => {
const form = attach(createForm())
const field = attach(
form.createField({
name: 'normal',
})
)

const validator1 = jest.fn(() => '')
const validator2 = jest.fn(() => '')
const validator3 = jest.fn(() => '')

field.setValidator([validator1, validator2, validator3])

await form.validate()
expect(validator1).toBeCalledTimes(1)
})
10 changes: 8 additions & 2 deletions packages/core/src/shared/internals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -949,8 +949,14 @@ export const validateSelf = batch.bound(
if (target.pattern !== 'editable' || target.display !== 'visible') return {}
start()
if (!triggerType) {
const allTriggerTypes = parseValidatorDescriptions(target.validator).map(
(desc) => desc.triggerType
const allTriggerTypes = parseValidatorDescriptions(
target.validator
).reduce(
(types, desc) =>
types.indexOf(desc.triggerType) > -1
? types
: types.concat(desc.triggerType),
[]
)
const results = {}
for (let i = 0; i < allTriggerTypes.length; i++) {
Expand Down

0 comments on commit 88d6f83

Please sign in to comment.