From cdca0db75a10525eca08696e8a44186cbe20c32a Mon Sep 17 00:00:00 2001 From: Jeroen de Bruijn <62570005+jdbruijn@users.noreply.github.com> Date: Thu, 14 Apr 2022 18:40:52 +0200 Subject: [PATCH] docs: infer `Literal` from the schema in JSON type example (#1083) --- README.md | 4 ++-- README_ZH.md | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 18ec943e1..eeff18b41 100644 --- a/README.md +++ b/README.md @@ -1185,9 +1185,9 @@ const Category: z.ZodType = BaseCategory.merge( If you want to validate any JSON value, you can use the snippet below. ```ts -type Literal = boolean | null | number | string; -type Json = Literal | { [key: string]: Json } | Json[]; const literalSchema = z.union([z.string(), z.number(), z.boolean(), z.null()]); +type Literal = z.infer; +type Json = Literal | { [key: string]: Json } | Json[]; const jsonSchema: z.ZodType = z.lazy(() => z.union([literalSchema, z.array(jsonSchema), z.record(jsonSchema)]) ); diff --git a/README_ZH.md b/README_ZH.md index 88fe0d22b..0ca2082c0 100644 --- a/README_ZH.md +++ b/README_ZH.md @@ -1012,9 +1012,9 @@ const Category: z.ZodSchema = BaseCategory.merge( 如果你想验证任何 JSON 值,你可以使用下面的片段。 ```ts -type Literal = boolean | null | number | string; -type Json = Literal | { [key: string]: Json } | Json[]; const literalSchema = z.union([z.string(), z.number(), z.boolean(), z.null()]); +type Literal = z.infer; +type Json = Literal | { [key: string]: Json } | Json[]; const jsonSchema: z.ZodSchema = z.lazy(() => z.union([literalSchema, z.array(jsonSchema), z.record(jsonSchema)]) );