diff --git a/README.md b/README.md index c2934e0e1..99bda0e3d 100644 --- a/README.md +++ b/README.md @@ -1033,7 +1033,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: @@ -1056,6 +1056,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 9c2061c8a..f12fd1582 100644 --- a/deno/lib/README.md +++ b/deno/lib/README.md @@ -1032,7 +1032,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: @@ -1055,6 +1055,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()`.