Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(validateFields): add message to reject error type #748

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

感觉你可以把错误都列一列,另外没有 test case

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果字段太多的话 message 可能会有过长的问题,另外多个字符串拼在一起,连字符选什么也需要考虑,还要考虑多语言,用中文或英文符号都不是很合适。因此我觉得还是最好只保留第一条,处理起来比较简单。

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已补充测试用例

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