40 lines
974 B
TypeScript
Raw Normal View History

2024-08-15 12:08:45 +02:00
import { type InferIssue } from "valibot";
2024-08-15 12:08:15 +02:00
type ValidationError = {
2024-08-15 12:08:45 +02:00
expected: string;
received: string;
message: string;
path: string;
};
2024-08-15 12:08:15 +02:00
/**
2024-08-15 12:08:45 +02:00
* The CodexError which can be error object of 3 types:
* `error`: Object containing the error message
* `api`: Object containing the api error message and the status code
* `validation`: Object containing the error message and a field `errors` of type ValidationError
* containing the error message for each fields.
2024-08-15 12:08:15 +02:00
*/
2024-08-15 12:08:45 +02:00
export type CodexError =
| {
type: "error";
message: string;
}
| {
type: "api";
message: string;
status: number;
}
| {
type: "validation";
message: string;
errors: ValidationError[];
};
2024-08-15 12:08:15 +02:00
2024-08-15 12:08:45 +02:00
export const CodexValibotIssuesMap = (issues: InferIssue<any>[]) =>
issues.map((i) => ({
expected: i.expected,
received: i.received,
message: i.message,
path: i.path.map((item: { key: string }) => item.key).join("."),
}));