From cb73743acf46acde89f86308d54e399c318f2bce Mon Sep 17 00:00:00 2001 From: Guo Yunhe Date: Wed, 22 Jan 2025 18:15:15 +0800 Subject: [PATCH] feat(validateFields): add message to reject error type (#748) --- src/interface.ts | 1 + src/useForm.ts | 2 ++ tests/index.test.tsx | 5 ++++- 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/interface.ts b/src/interface.ts index 751ed3b4..482f6d09 100644 --- a/src/interface.ts +++ b/src/interface.ts @@ -86,6 +86,7 @@ export type RuleObject = AggregationRule | ArrayRule; export type Rule = RuleObject | RuleRender; export interface ValidateErrorEntity { + message: string; values: Values; errorFields: { name: InternalNamePath; errors: string[] }[]; outOfDate: boolean; diff --git a/src/useForm.ts b/src/useForm.ts index fe8c8069..cb9c5f09 100644 --- a/src/useForm.ts +++ b/src/useForm.ts @@ -982,7 +982,9 @@ export class FormStore { }) .catch((results: { name: InternalNamePath; errors: string[] }[]) => { const errorList = results.filter(result => result && result.errors.length); + const errorMessage = errorList[0]?.errors?.[0]; return Promise.reject({ + message: errorMessage, values: this.getFieldsValue(namePathList), errorFields: errorList, outOfDate: this.lastValidatePromise !== summaryPromise, diff --git a/tests/index.test.tsx b/tests/index.test.tsx index 8cab35d8..c4a3deaa 100644 --- a/tests/index.test.tsx +++ b/tests/index.test.tsx @@ -328,9 +328,12 @@ describe('Form.Basic', () => { matchError(container, "'user' is required"); expect(onFinish).not.toHaveBeenCalled(); expect(onFinishFailed).toHaveBeenCalledWith({ + message: "'user' is required", errorFields: [{ name: ['user'], errors: ["'user' is required"], warnings: [] }], outOfDate: false, - values: {}, + values: { + user: undefined + }, }); onFinish.mockReset();