Skip to content

Commit 0a9cb81

Browse files
authored
fix: Async validator type check (#317)
1 parent aa14eea commit 0a9cb81

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

src/utils/validateUtil.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ async function validateRule(
3333
messageVariables?: Record<string, string>,
3434
): Promise<string[]> {
3535
const cloneRule = { ...rule };
36+
37+
// Bug of `async-validator`
38+
// https://github.com/react-component/field-form/issues/316
39+
// https://github.com/react-component/field-form/issues/313
40+
delete (cloneRule as any).ruleIndex;
41+
3642
// We should special handle array validate
3743
let subRuleField: RuleObject = null;
3844
if (cloneRule && cloneRule.type === 'array' && cloneRule.defaultField) {
@@ -218,13 +224,13 @@ export function validateRules(
218224
}
219225

220226
async function finishOnAllFailed(rulePromises: Promise<RuleError>[]): Promise<RuleError[]> {
221-
return Promise.all(rulePromises).then((errorsList: RuleError[]):
222-
| RuleError[]
223-
| Promise<RuleError[]> => {
224-
const errors: RuleError[] = [].concat(...errorsList);
227+
return Promise.all(rulePromises).then(
228+
(errorsList: RuleError[]): RuleError[] | Promise<RuleError[]> => {
229+
const errors: RuleError[] = [].concat(...errorsList);
225230

226-
return errors;
227-
});
231+
return errors;
232+
},
233+
);
228234
}
229235

230236
async function finishOnFirstFailed(rulePromises: Promise<RuleError>[]): Promise<RuleError[]> {

tests/validate.test.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,5 +726,17 @@ describe('Form.Validate', () => {
726726
const values = await form.validateFields(['username']);
727727
expect(values.username.do).toBe('');
728728
});
729+
730+
it('not trigger validator', async () => {
731+
const wrapper = mount(
732+
<div>
733+
<Form>
734+
<InfoField name="user" rules={[{ required: true }]} />
735+
</Form>
736+
</div>,
737+
);
738+
await changeValue(getField(wrapper, 0), ['light']);
739+
matchError(wrapper, false);
740+
});
729741
});
730742
/* eslint-enable no-template-curly-in-string */

0 commit comments

Comments
 (0)