( error: unknown, setError: UseFormSetError<TForm>, fields: readonly Path<TForm>[] )
| 3 | import { ApiError } from "@/lib/api/ApiError"; |
| 4 | |
| 5 | export function applyServerErrors<TForm extends FieldValues>( |
| 6 | error: unknown, |
| 7 | setError: UseFormSetError<TForm>, |
| 8 | fields: readonly Path<TForm>[] |
| 9 | ): boolean { |
| 10 | if (!(error instanceof ApiError) || !error.fieldErrors) { |
| 11 | return false; |
| 12 | } |
| 13 | |
| 14 | for (const [field, message] of Object.entries(error.fieldErrors)) { |
| 15 | /* |
| 16 | * Match the server key against the form's own path list. `find` yields a |
| 17 | * real `Path<TForm>` element (or nothing), so setError stays type-sound |
| 18 | * with no assertion — Path<TForm> has no runtime witness to cast to. |
| 19 | * Server keys that aren't form fields are ignored. |
| 20 | */ |
| 21 | const path = fields.find((candidate) => candidate === field); |
| 22 | |
| 23 | if (path !== undefined) { |
| 24 | setError(path, { type: "server", message }); |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | return true; |
| 29 | } |
no outgoing calls
no test coverage detected