Skip to content

Commit

Permalink
feat(validateFields): add message to reject error type (#748)
Browse files Browse the repository at this point in the history
  • Loading branch information
guoyunhe authored Jan 22, 2025
1 parent 24c7583 commit cb73743
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export type RuleObject = AggregationRule | ArrayRule;
export type Rule = RuleObject | RuleRender;

export interface ValidateErrorEntity<Values = any> {
message: string;
values: Values;
errorFields: { name: InternalNamePath; errors: string[] }[];
outOfDate: boolean;
Expand Down
2 changes: 2 additions & 0 deletions src/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 4 additions & 1 deletion tests/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit cb73743

Please sign in to comment.