Skip to content

Instantly share code, notes, and snippets.

@gregpalaci
Created November 3, 2023 00:38
Show Gist options
  • Save gregpalaci/7d00f84756ffd9f0b13f64619c98e672 to your computer and use it in GitHub Desktop.
Save gregpalaci/7d00f84756ffd9f0b13f64619c98e672 to your computer and use it in GitHub Desktop.
jsonSchema.ts
const literalSchema = z.union([z.string(), z.number(), z.boolean(), z.null()]);
type Literal = z.infer<typeof literalSchema>;
type Json = Literal | { [key: string]: Json } | Json[];
const jsonSchema: z.ZodType<Json> = z.lazy(() =>
z.union([literalSchema, z.array(jsonSchema), z.record(jsonSchema)])
);
jsonSchema.parse(data);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment