@ocubist/error-alchemy
Version:
A powerful Node.js error-handling-framework with custom error types for easy debugging, enhanced error management, strong zod-support and useful quality-of-life-functionality for asserting and validating values.
22 lines (16 loc) • 699 B
text/typescript
import z from "zod";
import { errorCodeSelector } from "./errorCodeSelector";
// Helper type to extract values from a type
type ValueOf<T> = T[keyof T];
// Derive the ErrorCode type from the values of errorCodeSelector
export type ErrorCode = ValueOf<typeof errorCodeSelector>;
// * Zod-Schema
// Extract the values from errorCodeSelector into an array
const errorCodeValues = Object.values(errorCodeSelector) as ErrorCode[];
// Cast errorCodeValues to a tuple type expected by Zod
const errorCodeTuple: [ErrorCode, ...ErrorCode[]] =
errorCodeValues as unknown as [ErrorCode, ...ErrorCode[]];
/**
* Zod schema for validating error codes.
*/
export const ErrorCode = z.enum(errorCodeTuple);