From 479f2e7338e2dbae9fc43dcf8a598e5592190bd7 Mon Sep 17 00:00:00 2001 From: "Crane.z" <1481445951@qq.com> Date: Mon, 23 Mar 2026 18:47:09 +0800 Subject: [PATCH] fix: improve readability of error messages --- src/components/Form/AutoDataForm/index.vue | 5 +++-- src/utils/common/index.js | 23 ++++++++++++++++++---- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/components/Form/AutoDataForm/index.vue b/src/components/Form/AutoDataForm/index.vue index 750462be5..45442ca64 100644 --- a/src/components/Form/AutoDataForm/index.vue +++ b/src/components/Form/AutoDataForm/index.vue @@ -30,6 +30,7 @@ import DataForm from '../DataForm/index.vue' import FormGroupHeader from '@/components/Form/FormGroupHeader/index.vue' import { FormFieldGenerator } from '@/components/Form/AutoDataForm/utils' import { UniqueCheck } from '@/components/Form/DataForm/rules' +import { joinErrorMessages } from '@/utils/common' export default { name: 'AutoDataForm', @@ -225,7 +226,7 @@ export default { let msg = v console.log(k, v) // v是数组并且数组都是字符串,则拼接为字符串 - if (Array.isArray(v) && v.every(item => typeof item === 'string')) msg = v.join('; ') + if (Array.isArray(v) && v.every(item => typeof item === 'string')) msg = joinErrorMessages(v) // 处理 [{"port":["请确保该值小于或者等于 65535。"]},{},{}] 这种情况 else if (Array.isArray(v) && v.every(item => _.isPlainObject(item))) { const subMsg = [] @@ -236,7 +237,7 @@ export default { } }) }) - msg = subMsg.join(' ') + msg = joinErrorMessages(subMsg, ' ') } else if (typeof v === 'object' && v !== null) msg = JSON.stringify(v) mapped[k] = String(msg || '') }) diff --git a/src/utils/common/index.js b/src/utils/common/index.js index 5a845f93d..8997b41b1 100644 --- a/src/utils/common/index.js +++ b/src/utils/common/index.js @@ -4,6 +4,22 @@ import { scopedLocalStorage as localStorage } from '@/utils/storage' const _ = require('lodash') +function trimTrailingPunctuation(message) { + return String(message || '').replace(/[;;\s]*[。.]+$/g, '').trim() +} + +export function joinErrorMessages(messages, separator = '; ') { + if (!Array.isArray(messages)) return '' + + const normalized = messages + .map(item => String(item || '').trim()) + .filter(Boolean) + + return normalized + .map((item, index) => index === normalized.length - 1 ? item : trimTrailingPunctuation(item)) + .join(separator) +} + export function getApiPath(that, objectId) { let pagePath = that.$route.path const pagePathArray = pagePath.split('/') @@ -142,12 +158,11 @@ export function getErrorResponseMsg(error) { } else if (data && data['non_field_errors']) { msg = data['non_field_errors'].join(' ') } else if (Array.isArray(data)) { - msg = data + msg = joinErrorMessages(data .map((item, i) => { return getErrorResponseMsg(item) }) - .filter(i => i) - .join('; ') + .filter(i => i)) } else if (typeof data === 'string') { return data } else if (_.isPlainObject(data)) { @@ -155,7 +170,7 @@ export function getErrorResponseMsg(error) { .map(item => getErrorResponseMsg(item)) .filter(i => i) // 错误信息不要重复提示 - return [...new Set(msg)].join('; ') + return joinErrorMessages([...new Set(msg)]) } else { msg = error.toString() }