39 lines
936 B
TypeScript
39 lines
936 B
TypeScript
import { z } from "zod";
|
|
|
|
type Assert<_T extends true> = void;
|
|
|
|
// -------- Encodable -------- //
|
|
|
|
export const all_encodable_tags = [
|
|
"Text",
|
|
"Timestamp",
|
|
"Uuid",
|
|
] as const;
|
|
|
|
// Type checking to ensure that all valid enum tags are included.
|
|
type _ = Assert<
|
|
Encodable["t"] extends (typeof all_encodable_tags)[number] ? true : false
|
|
>;
|
|
|
|
const encodable_text_schema = z.object({
|
|
t: z.literal("Text"),
|
|
c: z.string().nullish().transform((x) => x ?? undefined),
|
|
});
|
|
|
|
const encodable_timestamp_schema = z.object({
|
|
t: z.literal("Timestamp"),
|
|
c: z.coerce.date().nullish().transform((x) => x ?? undefined),
|
|
});
|
|
|
|
const encodable_uuid_schema = z.object({
|
|
t: z.literal("Uuid"),
|
|
c: z.string().nullish().transform((x) => x ?? undefined),
|
|
});
|
|
|
|
export const encodable_schema = z.union([
|
|
encodable_text_schema,
|
|
encodable_timestamp_schema,
|
|
encodable_uuid_schema,
|
|
]);
|
|
|
|
export type Encodable = z.infer<typeof encodable_schema>;
|