Created
November 3, 2023 00:38
-
-
Save jswhisperer/7d00f84756ffd9f0b13f64619c98e672 to your computer and use it in GitHub Desktop.
jsonSchema.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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