Files
Nexus/frontend/src/utils/validationErrorFormat.ts
T
Nexus Agent 091fb97291
Nexus CI/CD / test (push) Waiting to run
Nexus CI/CD / deploy (push) Blocked by required conditions
Nexus Pre-commit Checks / quick-check (push) Waiting to run
Push async submit, Chinese 422 field labels, hide server CSV export.
Return sync/files immediately with background runner; show loc_zh in validation errors; remove servers toolbar CSV export button.
2026-06-08 23:28:26 +08:00

38 lines
1.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/** Format FastAPI 422 validation detail items for display (Chinese). */
const LOC_PREFIX_SKIP = new Set(['body', 'query', 'path', 'header', 'cookie'])
export function formatValidationDetailItem(x: {
msg?: string
loc?: unknown[]
loc_zh?: string
}): string {
const part = (x?.msg || '').trim()
if (x?.loc_zh) {
return part ? `${x.loc_zh}${part}` : x.loc_zh
}
const loc = Array.isArray(x?.loc)
? x.loc.filter((p) => !LOC_PREFIX_SKIP.has(String(p)))
: []
const locStr = loc
.map((p) => (typeof p === 'number' ? `第${p + 1}项` : String(p)))
.join('.')
if (locStr && part) return `${locStr}${part}`
return part || locStr || JSON.stringify(x)
}
export function formatValidationDetail(detail: unknown, fallback: string): string {
if (typeof detail === 'string') {
const s = detail.trim()
return s || fallback
}
if (Array.isArray(detail)) {
const msg = detail.map((x) => formatValidationDetailItem(x as { msg?: string; loc?: unknown[]; loc_zh?: string })).join('')
return msg.trim() || fallback
}
if (detail && typeof detail === 'object') {
return JSON.stringify(detail)
}
return fallback
}