Skip to content

Commit 8d95757

Browse files
authored
fix: validateFirst should not always pass (#176)
* test driven * fix logic
1 parent d7fa224 commit 8d95757

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

src/utils/validateUtil.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,12 +192,12 @@ export function validateRules(
192192

193193
if (validateFirst === true) {
194194
// >>>>> Validate by serialization
195-
summaryPromise = new Promise(async resolve => {
195+
summaryPromise = new Promise(async (resolve, reject) => {
196196
/* eslint-disable no-await-in-loop */
197197
for (let i = 0; i < filledRules.length; i += 1) {
198198
const errors = await validateRule(name, value, filledRules[i], options, messageVariables);
199199
if (errors.length) {
200-
resolve(errors);
200+
reject(errors);
201201
return;
202202
}
203203
}

tests/validate.test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,38 @@ describe('Form.Validate', () => {
383383
await timeout();
384384
expect(onFinish).toHaveBeenCalledWith({ switch: false });
385385
});
386+
387+
it('validateFields should not pass when validateFirst is set', async () => {
388+
let form;
389+
390+
mount(
391+
<div>
392+
<Form
393+
ref={instance => {
394+
form = instance;
395+
}}
396+
>
397+
<InfoField name="user" validateFirst rules={[{ required: true }]}>
398+
<Input />
399+
</InfoField>
400+
</Form>
401+
</div>,
402+
);
403+
404+
// Validate callback
405+
await new Promise(resolve => {
406+
let failed = false;
407+
form
408+
.validateFields()
409+
.catch(() => {
410+
failed = true;
411+
})
412+
.then(() => {
413+
expect(failed).toBeTruthy();
414+
resolve();
415+
});
416+
});
417+
});
386418
});
387419

388420
it('should error in console if user script failed', async () => {

0 commit comments

Comments
 (0)