1
0
Fork 0
forked from 2sys/phonograph
phonograph/svelte/src/datum.svelte.ts

38 lines
853 B
TypeScript
Raw Normal View History

2025-09-08 15:56:57 -07:00
import { z } from "zod";
type Assert<_T extends true> = void;
export const all_datum_tags = [
2025-09-08 15:56:57 -07:00
"Text",
"Timestamp",
"Uuid",
] as const;
// Type checking to ensure that all valid enum tags are included.
type _ = Assert<
Datum["t"] extends (typeof all_datum_tags)[number] ? true : false
2025-09-08 15:56:57 -07:00
>;
const datum_text_schema = z.object({
2025-09-08 15:56:57 -07:00
t: z.literal("Text"),
c: z.string().nullish().transform((x) => x ?? undefined),
});
const datum_timestamp_schema = z.object({
2025-09-08 15:56:57 -07:00
t: z.literal("Timestamp"),
c: z.coerce.date().nullish().transform((x) => x ?? undefined),
});
const datum_uuid_schema = z.object({
2025-09-08 15:56:57 -07:00
t: z.literal("Uuid"),
c: z.string().nullish().transform((x) => x ?? undefined),
});
export const datum_schema = z.union([
datum_text_schema,
datum_timestamp_schema,
datum_uuid_schema,
2025-09-08 15:56:57 -07:00
]);
export type Datum = z.infer<typeof datum_schema>;