Skip to content

Commit

Permalink
Add documentation to Zod enum exclude/extract functions (#3044)
Browse files Browse the repository at this point in the history
* Add documentation to Zod enum exclude/extract functions

* Tweak

---------

Co-authored-by: Colin McDonnell <[email protected]>
  • Loading branch information
shaharke and colinhacks authored Feb 3, 2024
1 parent fd61644 commit 7e9f3a4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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()`.
Expand Down
12 changes: 11 additions & 1 deletion deno/lib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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()`.
Expand Down

0 comments on commit 7e9f3a4

Please sign in to comment.