* Join the `msg` fields of a FastAPI/Pydantic validation `detail` array * (`[{ loc, msg, type }, ...]`), or return null when there are none. With * several errors each message is prefixed with its field so they can be * told apart.
(detail: unknown)
| 32 | * told apart. |
| 33 | */ |
| 34 | function getValidationDetailMessage(detail: unknown): string | null { |
| 35 | if (!Array.isArray(detail)) return null; |
| 36 | const entries = detail |
| 37 | .filter( |
| 38 | (item): item is { loc?: unknown; msg: string } => |
| 39 | !!item && |
| 40 | typeof item === "object" && |
| 41 | typeof (item as { msg?: unknown }).msg === "string" && |
| 42 | (item as { msg: string }).msg !== "", |
| 43 | ) |
| 44 | .map(({ loc, msg }) => ({ field: getValidationField(loc), msg })); |
| 45 | if (entries.length === 0) return null; |
| 46 | if (entries.length === 1) return entries[0].msg; |
| 47 | return entries |
| 48 | .map(({ field, msg }) => (field ? `${field}: ${msg}` : msg)) |
| 49 | .join("; "); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Join the messages of a structured `detail.errors` array |
no test coverage detected