mirror of
https://gitee.com/ByteDance/flowgram.ai.git
synced 2025-07-07 17:43:29 +08:00
38 lines
788 B
TypeScript
38 lines
788 B
TypeScript
import {
|
|
Errors,
|
|
Feedback,
|
|
FeedbackLevel,
|
|
FieldError,
|
|
FieldName,
|
|
FieldWarning,
|
|
FormErrorOptions,
|
|
FormWarningOptions,
|
|
} from '../types';
|
|
|
|
export function toFeedback(
|
|
result: string | FormErrorOptions | FormWarningOptions | undefined,
|
|
name: FieldName
|
|
): FieldError | FieldWarning | undefined {
|
|
if (typeof result === 'string') {
|
|
return {
|
|
name,
|
|
message: result,
|
|
level: FeedbackLevel.Error,
|
|
};
|
|
} else if (result?.message) {
|
|
return {
|
|
...result,
|
|
name,
|
|
};
|
|
}
|
|
}
|
|
|
|
export function feedbackToFieldErrorsOrWarnings<T>(name: string, feedback?: Feedback<any>) {
|
|
return {
|
|
[name]: feedback ? [feedback] : [],
|
|
} as T;
|
|
}
|
|
|
|
export const hasError = (errors: Errors) =>
|
|
Object.keys(errors).some((key) => errors[key]?.length > 0);
|