mirror of
https://gitee.com/ByteDance/flowgram.ai.git
synced 2025-07-07 17:43:29 +08:00
fix(form): fix hasError called in FormModel.validate
This commit is contained in:
parent
b4450db944
commit
40eafff3ec
22
packages/node-engine/form/__tests__/validate.test.ts
Normal file
22
packages/node-engine/form/__tests__/validate.test.ts
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
import { describe, expect, it } from 'vitest';
|
||||||
|
|
||||||
|
import { hasError } from '../src/utils/validate';
|
||||||
|
import { FeedbackLevel, FieldError } from '../src/types';
|
||||||
|
|
||||||
|
describe('utils/validate', () => {
|
||||||
|
describe('hasError', () => {
|
||||||
|
it('should return false when errors is empty', () => {
|
||||||
|
expect(hasError({ xxx: [] })).toBe(false);
|
||||||
|
expect(hasError({ xxx: undefined })).toBe(false);
|
||||||
|
expect(hasError({})).toBe(false);
|
||||||
|
expect(hasError({ aaa: [], bbb: [] })).toBe(false);
|
||||||
|
expect(hasError({ aaa: undefined, bbb: [] })).toBe(false);
|
||||||
|
});
|
||||||
|
it('should return true when errors is not empty', () => {
|
||||||
|
const mockError: FieldError = { name: 'xxx', level: FeedbackLevel.Error, message: 'err' };
|
||||||
|
expect(hasError({ xxx: [mockError] })).toBe(true);
|
||||||
|
expect(hasError({ aaa: [mockError], bbb: [mockError] })).toBe(true);
|
||||||
|
expect(hasError({ aaa: undefined, bbb: [mockError] })).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@ -279,6 +279,7 @@ export class FormModel<TValues = any> implements Disposable {
|
|||||||
const warnings = feedbackToFieldErrorsOrWarnings<Warnings>(path, feedback);
|
const warnings = feedbackToFieldErrorsOrWarnings<Warnings>(path, feedback);
|
||||||
|
|
||||||
if (field) {
|
if (field) {
|
||||||
|
debugger;
|
||||||
field.state.errors = errors;
|
field.state.errors = errors;
|
||||||
field.state.warnings = warnings;
|
field.state.warnings = warnings;
|
||||||
field.state.invalid = hasError(errors);
|
field.state.invalid = hasError(errors);
|
||||||
|
|||||||
@ -33,11 +33,5 @@ export function feedbackToFieldErrorsOrWarnings<T>(name: string, feedback?: Feed
|
|||||||
} as T;
|
} as T;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const hasError = (errors: Errors) => {
|
export const hasError = (errors: Errors) =>
|
||||||
for (let fieldErrors in errors) {
|
Object.keys(errors).some((key) => errors[key]?.length > 0);
|
||||||
if (fieldErrors.length) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
};
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user