From 1201ad143e87c21153a2fea6ca69d4a884c209f8 Mon Sep 17 00:00:00 2001 From: Shahar Kedar Date: Tue, 12 Dec 2023 08:35:08 +0200 Subject: [PATCH 1/2] Add documentation to Zod enum exclude/extract functions --- README.md | 10 ++++++++++ deno/lib/README.md | 10 ++++++++++ 2 files changed, 20 insertions(+) diff --git a/README.md b/README.md index 4bd1f9039..9e41f55bd 100644 --- a/README.md +++ b/README.md @@ -1012,6 +1012,16 @@ You can also retrieve the list of options as a tuple with the `.options` propert FishEnum.options; // ["Salmon", "Tuna", "Trout"]; ``` +**Exclude/Extract** + +You can create subsets of a Zod enum with the `.exclude` and `.extract` methods. + +```ts +const FishEnum = z.enum(["Salmon", "Tuna", "Trout"]); +const SalmonAndTrout = FishEnum.extract(["Salmon", "Trout"]); +const TunaOnly = FishEnum.exclude(["Salmon", "Trout"]); +``` + ## Native enums Zod enums are the recommended approach to defining and validating enums. But if you need to validate against an enum from a third-party library (or you don't want to rewrite your existing enums) you can use `z.nativeEnum()`. diff --git a/deno/lib/README.md b/deno/lib/README.md index 4bd1f9039..9e41f55bd 100644 --- a/deno/lib/README.md +++ b/deno/lib/README.md @@ -1012,6 +1012,16 @@ You can also retrieve the list of options as a tuple with the `.options` propert FishEnum.options; // ["Salmon", "Tuna", "Trout"]; ``` +**Exclude/Extract** + +You can create subsets of a Zod enum with the `.exclude` and `.extract` methods. + +```ts +const FishEnum = z.enum(["Salmon", "Tuna", "Trout"]); +const SalmonAndTrout = FishEnum.extract(["Salmon", "Trout"]); +const TunaOnly = FishEnum.exclude(["Salmon", "Trout"]); +``` + ## Native enums Zod enums are the recommended approach to defining and validating enums. But if you need to validate against an enum from a third-party library (or you don't want to rewrite your existing enums) you can use `z.nativeEnum()`. From ebabe3aad8f801e8cf68c5da06e29c957a5ea8a4 Mon Sep 17 00:00:00 2001 From: Colin McDonnell Date: Fri, 2 Feb 2024 16:13:20 -0800 Subject: [PATCH 2/2] Tweak --- README.md | 4 ++-- deno/lib/README.md | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 9e41f55bd..04f9d9db0 100644 --- a/README.md +++ b/README.md @@ -989,7 +989,7 @@ const fish = ["Salmon", "Tuna", "Trout"]; const FishEnum = z.enum(fish); ``` -**Autocompletion** +**`.enum`** To get autocompletion with a Zod enum, use the `.enum` property of your schema: @@ -1012,7 +1012,7 @@ You can also retrieve the list of options as a tuple with the `.options` propert FishEnum.options; // ["Salmon", "Tuna", "Trout"]; ``` -**Exclude/Extract** +**`.exclude/.extract()`** You can create subsets of a Zod enum with the `.exclude` and `.extract` methods. diff --git a/deno/lib/README.md b/deno/lib/README.md index 9e41f55bd..04f9d9db0 100644 --- a/deno/lib/README.md +++ b/deno/lib/README.md @@ -989,7 +989,7 @@ const fish = ["Salmon", "Tuna", "Trout"]; const FishEnum = z.enum(fish); ``` -**Autocompletion** +**`.enum`** To get autocompletion with a Zod enum, use the `.enum` property of your schema: @@ -1012,7 +1012,7 @@ You can also retrieve the list of options as a tuple with the `.options` propert FishEnum.options; // ["Salmon", "Tuna", "Trout"]; ``` -**Exclude/Extract** +**`.exclude/.extract()`** You can create subsets of a Zod enum with the `.exclude` and `.extract` methods.