Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
colinhacks committed Feb 20, 2025
1 parent 76bbfed commit 78dd95e
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<br/>

<!-- ## Table of contents -->
<br/>
<h2 align="center">Table of contents</h2>

> These docs have been translated into [Chinese](./README_ZH.md) and [Korean](./README_KO.md).
Expand Down Expand Up @@ -180,6 +181,7 @@
- [Changelog](#changelog)

<!-- ## Introduction -->
<br/>
<h2 align="center">Introduction</h2>

Zod is a TypeScript-first schema declaration and validation library. I'm using the term "schema" to broadly refer to any data type, from a simple `string` to a complex nested object.
Expand All @@ -197,6 +199,7 @@ Some other great aspects:
- Works with plain JavaScript too! You don't need to use TypeScript.

<!-- ## Sponsors -->
<br/>
<h2 align="center">Sponsors</h2>

Sponsorship at any level is appreciated and encouraged. If you built a paid product using Zod, consider one of the [corporate tiers](https://github.com/sponsors/colinhacks).
Expand Down Expand Up @@ -616,6 +619,7 @@ There are a growing number of tools that are built atop or support Zod natively!
- [`zod-accelerator`](https://github.com/duplojs/duplojs-zod-accelerator): Accelerates Zod's throughput up to ~100x.

<!-- ## Installation -->
<br/>
<h2 align="center">Installation</h2>

### Requirements
Expand Down Expand Up @@ -657,6 +661,7 @@ pnpm add zod@canary # pnpm
> The rest of this README assumes you are using npm and importing directly from the `"zod"` package.
<!-- ## Basic usage -->
<br/>
<h2 align="center">Basic usage</h2>

Creating a simple string schema
Expand Down Expand Up @@ -693,6 +698,7 @@ type User = z.infer<typeof User>;
```

<!-- ## Primitives -->
<br/>
<h2 align="center">Primitives</h2>

```ts
Expand Down Expand Up @@ -722,6 +728,7 @@ z.never();
```

<!-- ## Coercion for primitives -->
<br/>
<h2 align="center">Coercion for primitives</h2>

Zod now provides a more convenient way to coerce primitive values.
Expand Down Expand Up @@ -779,6 +786,7 @@ schema.parse(null); // => false
For more control over coercion logic, consider using [`z.preprocess`](#preprocess) or [`z.pipe()`](#pipe).

<!-- ## Literals -->
<br/>
<h2 align="center">Literals</h2>

Literal schemas represent a [literal type](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#literal-types), like `"hello world"` or `5`.
Expand All @@ -799,6 +807,7 @@ tuna.value; // "tuna"
> Currently there is no support for Date literals in Zod. If you have a use case for this feature, please file an issue.
<!-- ## Strings -->
<br/>
<h2 align="center">Strings</h2>

Zod includes a handful of string-specific validations.
Expand Down Expand Up @@ -998,6 +1007,7 @@ ipv6Cidr.parse("192.168.1.1"); // fail
```

<!-- ## Numbers -->
<br/>
<h2 align="center">Numbers</h2>

You can customize certain error messages when creating a number schema.
Expand Down Expand Up @@ -1037,6 +1047,7 @@ z.number().lte(5, { message: "this👏is👏too👏big" });
```

<!-- ## BigInts -->
<br/>
<h2 align="center">BigInts</h2>

Zod includes a handful of bigint-specific validations.
Expand All @@ -1056,6 +1067,7 @@ z.bigint().multipleOf(5n); // Evenly divisible by 5n.
```

<!-- ## NaNs -->
<br/>
<h2 align="center">NaNs</h2>

You can customize certain error messages when creating a nan schema.
Expand All @@ -1068,6 +1080,7 @@ const isNaN = z.nan({
```

<!-- ## Booleans -->
<br/>
<h2 align="center">Booleans</h2>

You can customize certain error messages when creating a boolean schema.
Expand All @@ -1080,6 +1093,7 @@ const isActive = z.boolean({
```

<!-- ## Dates -->
<br/>
<h2 align="center">Dates</h2>

Use z.date() to validate `Date` instances.
Expand Down Expand Up @@ -1128,6 +1142,7 @@ console.log(dateSchema.safeParse("0000-00-00").success); // false
For older zod versions, use [`z.preprocess`](#preprocess) like [described in this thread](https://github.com/colinhacks/zod/discussions/879#discussioncomment-2036276).

<!-- ## Zod enums -->
<br/>
<h2 align="center">Zod enums</h2>

```ts
Expand Down Expand Up @@ -1184,6 +1199,7 @@ const TunaOnly = FishEnum.exclude(["Salmon", "Trout"]);
```

<!-- ## Native enums -->
<br/>
<h2 align="center">Native enums</h2>

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 Expand Up @@ -1253,6 +1269,7 @@ FruitEnum.enum.Apple; // "apple"
```

<!-- ## Optionals -->
<br/>
<h2 align="center">Optionals</h2>

You can make any schema optional with `z.optional()`. This wraps the schema in a `ZodOptional` instance and returns the result.
Expand Down Expand Up @@ -1282,6 +1299,7 @@ optionalString.unwrap() === stringSchema; // true
```

<!-- ## Nullables -->
<br/>
<h2 align="center">Nullables</h2>

Similarly, you can create nullable types with `z.nullable()`.
Expand All @@ -1308,6 +1326,7 @@ nullableString.unwrap() === stringSchema; // true
```

<!-- ## Objects -->
<br/>
<h2 align="center">Objects</h2>

```ts
Expand Down Expand Up @@ -1577,6 +1596,7 @@ person.parse({
Using `.catchall()` obviates `.passthrough()` , `.strip()` , or `.strict()`. All keys are now considered "known".

<!-- ## Arrays -->
<br/>
<h2 align="center">Arrays</h2>

```ts
Expand Down Expand Up @@ -1634,6 +1654,7 @@ z.string().array().length(5); // must contain 5 items exactly
Unlike `.nonempty()` these methods do not change the inferred type.

<!-- ## Tuples -->
<br/>
<h2 align="center">Tuples</h2>

Unlike arrays, tuples have a fixed number of elements and each element can have a different type.
Expand All @@ -1660,6 +1681,7 @@ const result = variadicTuple.parse(["hello", 1, 2, 3]);
```

<!-- ## Unions -->
<br/>
<h2 align="center">Unions</h2>

Zod includes a built-in `z.union` method for composing "OR" types.
Expand Down Expand Up @@ -1696,6 +1718,7 @@ console.log(optionalUrl.safeParse("not a valid url").success); // false
```

<!-- ## Discriminated unions -->
<br/>
<h2 align="center">Discriminated unions</h2>

A discriminated union is a union of object schemas that all share a particular key.
Expand Down Expand Up @@ -1739,6 +1762,7 @@ const AB = z.discriminatedUnion("status", [...A.options, ...B.options]);
```

<!-- ## Records -->
<br/>
<h2 align="center">Records</h2>

Record schemas are used to validate types such as `Record<string, number>`. This is particularly useful for storing or caching items by ID.
Expand Down Expand Up @@ -1794,6 +1818,7 @@ for (const key in testMap) {
As you can see, JavaScript automatically casts all object keys to strings under the hood. Since Zod is trying to bridge the gap between static and runtime types, it doesn't make sense to provide a way of creating a record schema with numerical keys, since there's no such thing as a numerical key in runtime JavaScript.

<!-- ## Maps -->
<br/>
<h2 align="center">Maps</h2>

```ts
Expand All @@ -1804,6 +1829,7 @@ type StringNumberMap = z.infer<typeof stringNumberMap>;
```

<!-- ## Sets -->
<br/>
<h2 align="center">Sets</h2>

```ts
Expand All @@ -1822,6 +1848,7 @@ z.set(z.string()).size(5); // must contain 5 items exactly
```

<!-- ## Intersections -->
<br/>
<h2 align="center">Intersections</h2>

Intersections are useful for creating "logical AND" types. This is useful for intersecting two object types.
Expand Down Expand Up @@ -1871,6 +1898,7 @@ type Teacher = z.infer<typeof Teacher>;
``` -->

<!-- ## Recursive types -->
<br/>
<h2 align="center">Recursive types</h2>

You can define a recursive schema in Zod, but because of a limitation of TypeScript, their type can't be statically inferred. Instead you'll need to define the type definition manually, and provide it to Zod as a "type hint".
Expand Down Expand Up @@ -1962,6 +1990,7 @@ Despite supporting recursive schemas, passing cyclical data into Zod will cause
> To detect cyclical objects before they cause problems, consider [this approach](https://gist.github.com/colinhacks/d35825e505e635df27cc950776c5500b).
<!-- ## Promises -->
<br/>
<h2 align="center">Promises</h2>

```ts
Expand Down Expand Up @@ -1994,6 +2023,7 @@ const test = async () => {
When "parsing" a promise, Zod checks that the passed value is an object with `.then` and `.catch` methods — that's it. So you should be able to pass non-native Promises (Bluebird, etc) into `z.promise(...).parse` with no trouble. One gotcha: the return type of the parse function will be a _native_ `Promise` , so if you have downstream logic that uses non-standard Promise methods, this won't work. -->

<!-- ## Instanceof -->
<br/>
<h2 align="center">Instanceof</h2>

You can use `z.instanceof` to check that the input is an instance of a class. This is useful to validate inputs against classes that are exported from third-party libraries.
Expand All @@ -2011,6 +2041,7 @@ TestSchema.parse(blob); // throws
```

<!-- ## Functions -->
<br/>
<h2 align="center">Functions</h2>

Zod also lets you define "function schemas". This makes it easy to validate the inputs and outputs of a function without intermixing your validation code and "business logic".
Expand Down Expand Up @@ -2095,6 +2126,7 @@ myFunction.returnType();
* `returnType: any Zod schema` The second argument is the function's return type. This can be any Zod schema. -->

<!-- ## Preprocess -->
<br/>
<h2 align="center">Preprocess</h2>

> Zod now supports primitive coercion without the need for `.preprocess()`. See the [coercion docs](#coercion-for-primitives) for more information.
Expand All @@ -2110,6 +2142,7 @@ const castToString = z.preprocess((val) => String(val), z.string());
This returns a `ZodEffects` instance. `ZodEffects` is a wrapper class that contains all logic pertaining to preprocessing, refinements, and transforms.

<!-- ## Custom schemas -->
<br/>
<h2 align="center">Custom schemas</h2>

You can create a Zod schema for any TypeScript type by using `z.custom()`. This is useful for creating schemas for types that are not supported by Zod out of the box, such as template string literals.
Expand Down Expand Up @@ -2138,6 +2171,7 @@ z.custom<...>((val) => ..., "custom error message");
```

<!-- ## Schema methods -->
<br/>
<h2 align="center">Schema methods</h2>

All Zod schemas contain certain methods.
Expand Down Expand Up @@ -2780,6 +2814,7 @@ console.log(toBigInt.safeParse(null).success); // false
```

<!-- ## Guides and concepts -->
<br/>
<h2 align="center">Guides and concepts</h2>

### Type inference
Expand Down Expand Up @@ -2950,6 +2985,7 @@ if (!result.success) {
```

<!-- ## Comparison -->
<br/>
<h2 align="center">Comparison</h2>

There are a handful of other widely-used validation libraries, but all of them have certain design limitations that make for a non-ideal developer experience.
Expand Down Expand Up @@ -3097,6 +3133,7 @@ Ow is focused on function input validation. It's a library that makes it easy to
If you want to validate function inputs, use function schemas in Zod! It's a much simpler approach that lets you reuse a function type declaration without repeating yourself (namely, copy-pasting a bunch of ow assertions at the beginning of every function). Also Zod lets you validate your return types as well, so you can be sure there won't be any unexpected data passed downstream.

<!-- ## Changelog -->
<br/>
<h2 align="center">Changelog</h2>

View the changelog at [CHANGELOG.md](CHANGELOG.md)
Loading

0 comments on commit 78dd95e

Please sign in to comment.