30 lines
818 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;
2024-09-13 19:45:59 +02:00
path: string | undefined;
2024-08-15 12:08:45 +02:00
};
2024-08-15 12:08:15 +02:00
/**
2024-08-15 12:08:48 +02:00
* The CodexError contains a message and 3 optionals properties:
* `code`: The (http) code error when it comes from a request
* `errors`: A {ValidationError} array when it comes from an object validation process
* `stack`: The error stack when the CodexError results from a error thrown
2024-08-15 12:08:15 +02:00
*/
2024-08-15 12:08:48 +02:00
export type CodexError = {
message: string;
code?: number;
errors?: ValidationError[];
stack?: string;
};
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,
2024-08-15 12:08:51 +02:00
path: i.path?.map((item: { key: string }) => item.key).join("."),
2024-08-15 12:08:45 +02:00
}));