DRY up ZodError → FieldError mapping
Five journal API route handlers had the same inline lambda:
parsed.error.issues.map((i) => ({
field: i.path.join("."),
message: i.message,
}))
Pull it into `zodIssuesToFieldErrors(error)` next to FieldError in
`@trails-cool/api/errors.ts` and re-export it so all five handlers
share the same implementation. The path-flattening rule now lives in
one place, and new routes get it for free.
Public error-response shape is unchanged.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
079c3de90c
commit
edd2cabf12
7 changed files with 23 additions and 8 deletions
|
|
@ -14,6 +14,18 @@ export const ApiErrorResponseSchema = z.object({
|
|||
export type FieldError = z.infer<typeof FieldErrorSchema>;
|
||||
export type ApiErrorResponse = z.infer<typeof ApiErrorResponseSchema>;
|
||||
|
||||
/**
|
||||
* Map a ZodError's issues to the FieldError[] shape used in API
|
||||
* validation responses. Keeps the path-flattening rule in one place so
|
||||
* every route returns validation errors in the same shape.
|
||||
*/
|
||||
export function zodIssuesToFieldErrors(error: z.ZodError): FieldError[] {
|
||||
return error.issues.map((i) => ({
|
||||
field: i.path.join("."),
|
||||
message: i.message,
|
||||
}));
|
||||
}
|
||||
|
||||
/** Standard error codes */
|
||||
export const ERROR_CODES = {
|
||||
VALIDATION_ERROR: "VALIDATION_ERROR",
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ export { ENDPOINTS } from "./endpoints.ts";
|
|||
// Error schemas
|
||||
export {
|
||||
ApiErrorResponseSchema, FieldErrorSchema, ERROR_CODES,
|
||||
zodIssuesToFieldErrors,
|
||||
type ApiErrorResponse, type FieldError,
|
||||
} from "./errors.ts";
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue