Comparing version 3.0.0-alpha.6 to 3.0.0-alpha.7
@@ -51,3 +51,3 @@ # Changelog | ||
- Major overhaul to error handling system, including the introduction of custom error maps | ||
- Wrote new [error handling guide](https://github.com/colinhacks/zod/blob/master/ERROR_HANDLING.md) | ||
- Wrote new [error handling guide](./ERROR_HANDLING.md) | ||
@@ -54,0 +54,0 @@ ### zod@1.7 |
{ | ||
"name": "zod", | ||
"version": "3.0.0-alpha.6", | ||
"version": "3.0.0-alpha.7", | ||
"description": "TypeScript-first schema declaration and validation library with static type inference", | ||
@@ -8,2 +8,6 @@ "main": "./lib/cjs/index.js", | ||
"module": "./lib/esm/index.js", | ||
"exports": { | ||
"require": "./lib/cjs/index.js", | ||
"import": "./lib/esm/index.js" | ||
}, | ||
"files": [ | ||
@@ -77,3 +81,3 @@ "/lib" | ||
"hooks": { | ||
"pre-commit": "lint-staged" | ||
"pre-commit": "yarn build:deno && lint-staged" | ||
} | ||
@@ -80,0 +84,0 @@ }, |
106
README.md
@@ -78,15 +78,14 @@ <p align="center"> | ||
# Zod v3 is in alpha | ||
## Zod v3 is in alpha | ||
### New features | ||
#### New features | ||
- Transformers! But better! See the "breaking changes" section to understand the syntax changes. | ||
- You can now import Zod like `import { z } a from 'zod';` instead of using `import * as` syntax. | ||
- ZodError now has a method to format the error into a strongly-typed, nested object: `error.format()` | ||
- You can quickly create union types using the `or` method: `z.string().or(z.number())` | ||
- You can easily customize or localize Zod's built-in error message generation with `z.setErrorMap` | ||
- Added the `format` method to ZodError to convert the error into a strongly-typed, nested object: [format method](#error-formatting) | ||
- Added the `or` method to ZodType (the base class for all Zod schema) to easily create union types like `z.string().or(z.number())` | ||
- Added `z.setErrorMap`, an easier way to _globally_ customize the error messages produced by Zod: [setErrorMap](ERROR_HANDLING.md#customizing-errors-with-zoderrormap) | ||
- ZodOptional and ZodNullable now have a `.unwrap()` method for retrieving the schema they wrap | ||
- More intuitive `.default()` behavior | ||
### Breaking changes in v3 | ||
#### Breaking changes in v3 | ||
@@ -97,10 +96,15 @@ - The **minimum TypeScript version** is now _4.1_ (up from 3.7 for Zod 2). Several features have been rewritten to use [recursive conditional types](https://devblogs.microsoft.com/typescript/announcing-typescript-4-1/#recursive-conditional-types), an incredibly powerful new feature introduced in TS4.1. | ||
⚠️ The old syntax (`z.transformer(A, B, func)`) is no longer available. | ||
⚠️ The convenience method `A.transform(B, func)` is no longer available. | ||
The old syntax (`z.transformer(A, B, func)`) is no longer available. | ||
Instead, you apply transformations by simply using the `.transform()` method that exists on all Zod schemas. For example: `z.string().transform(val => val.length)`. | ||
The convenience method `A.transform(B, func)` is no longer available. | ||
Under the hood, all refinements and transformations are executed inside a dedicated "ZodEffects" class. Post-parsing, ZodEffects passes the data through a chain of refinements and transformations, then returns the final value. As such, you can now _interleave_ transformations and refinements. For instance: | ||
Instead, you apply transformations by simply using the `.transform()` method that exists on all Zod schemas. | ||
```ts | ||
z.string().transform((val) => val.length); | ||
``` | ||
- Under the hood, all refinements and transformations are executed inside a dedicated "ZodEffects" class. Post-parsing, ZodEffects passes the data through a chain of refinements and transformations, then returns the final value. As such, you can now _interleave_ transformations and refinements. For instance: | ||
```ts | ||
const test = z | ||
@@ -116,10 +120,10 @@ .string() | ||
- **Type guards** (the `.check()` method) have been removed. Type guards interact with transformers in unintuitive ways so they were removed. Use `.safeParse` instead. | ||
- **ZodIntersection has been removed**. If you have an object schema, you can use the .merge() method instead. Note that this is equivalent to `A.extend(B.shape)` and is therefore not an intersection in the pure sense (`B` takes precedence over `A` if the two schemas share a key). | ||
- **ZodIntersection has been removed**. If you have an object schema, you can use the `A.merge(B)` instead. Note that this is equivalent to `A.extend(B.shape)` and is therefore not an intersection in the pure sense, as `B` takes precedence over `A` if the two schemas share a key. | ||
- There have been small internal changes to the ZodIssue type. This may impact user who have written a custom error maps. Most users will not be affected. | ||
### Migrating from v1 | ||
#### Migrating from v1 | ||
If you're upgrading straight to v3 from v1, you'll need to be aware of the breaking changes introduced in both v2 and v3. The v1->v2 migration guide is [here](https://github.com/colinhacks/zod/tree/v2). | ||
If you're upgrading straight to v3 from v1, you'll need to be aware of the breaking changes introduced in both v2 and v3. The v1->v2 migration guide is [here](https://github.com/colinhacks/zod/tree/v2#migration-from-v1). | ||
### Migrating from v2 | ||
#### Migrating from v2 | ||
@@ -509,3 +513,3 @@ Zod 2 is being retired and will not leave beta. This is due to some issues with it's implementation of transformers: details [here](https://github.com/colinhacks/zod/issues/264). Zod 3 is currently in alpha — install it at `zod@next`. (Zod 2 will continue to be available with `zod@beta` for the time being.) | ||
### `strict` | ||
### `.strict` | ||
@@ -532,3 +536,3 @@ You can _disallow_ unknown keys with `.strict()` . If there are any unknown keys in the input, Zod will throw an error. | ||
### `.catchall()` | ||
### `.catchall` | ||
@@ -560,23 +564,10 @@ You can pass a "catchall" schema into an object schema. All unknown keys will be validated against it. | ||
There are two ways to define array schemas: | ||
#### `z.array(arg: ZodSchema)` | ||
First, you can create an array schema with the `z.array()` function; it accepts another ZodSchema, which defines the type of each array element. | ||
```ts | ||
const stringArray = z.array(z.string()); | ||
// inferred type: string[] | ||
``` | ||
#### the `.array()` method | ||
For convenience, you can also call the `.array()` method on **any** Zod schema: | ||
```ts | ||
// equivalent | ||
const stringArray = z.string().array(); | ||
// inferred type: string[] | ||
``` | ||
You have to be careful with the `.array()` method. It returns a new `ZodArray` instance. This means you need to be careful about the _order_ in which you call methods. These two schemas are very different: | ||
Be careful with the `.array()` method. It returns a new `ZodArray` instance. This means the _order_ in which you call methods matters. For instance: | ||
@@ -604,6 +595,5 @@ ```ts | ||
```ts | ||
// must contain 5 or more items | ||
z.string().array().min(5); | ||
z.string().array().max(5); | ||
z.string().array().length(5); | ||
z.string().array().min(5); // must contain 5 or more items | ||
z.string().array().max(5); // must contain 5 or fewer items | ||
z.string().array().length(5); // must contain 5 items exactly | ||
``` | ||
@@ -637,14 +627,12 @@ | ||
```ts | ||
const A = z.optional(z.string()); | ||
const schema = z.optional(z.string()); | ||
A.parse(undefined); // => passes, returns undefined | ||
schema.parse(undefined); // => returns undefined | ||
type A = z.infer<typeof A>; // string | undefined | ||
``` | ||
You can also call the `.optional()` method on an existing schema: | ||
You can make an existing schema optional with the `.optional()` method: | ||
```ts | ||
const B = z.boolean().optional(); | ||
const C = z.object({ | ||
const user = z.object({ | ||
username: z.string().optional(), | ||
@@ -655,2 +643,10 @@ }); | ||
#### `.unwrap` | ||
```ts | ||
const stringSchema = z.string(); | ||
const optionalString = stringSchema.optional(); | ||
optionalString.unwrap() === stringSchema; // true | ||
``` | ||
## Nullables | ||
@@ -661,8 +657,8 @@ | ||
```ts | ||
const D = z.nullable(z.string()); | ||
D.parse("asdf"); // => "asdf" | ||
D.parse(null); // => null | ||
const nullableString = z.nullable(z.string()); | ||
nullableString.parse("asdf"); // => "asdf" | ||
nullableString.parse(null); // => null | ||
``` | ||
Or you can use the `.nullable()` method on any existing schema: | ||
You can make an existing schema nullable with the `nullable` method: | ||
@@ -674,4 +670,10 @@ ```ts | ||
You can create unions of any two or more schemas. | ||
#### `.unwrap` | ||
```ts | ||
const stringSchema = z.string(); | ||
const nullableString = stringSchema.nullable(); | ||
nullableString.unwrap() === stringSchema; // true | ||
``` | ||
<!-- | ||
@@ -1112,3 +1114,3 @@ | ||
### `parseAsync` | ||
### `.parseAsync` | ||
@@ -1338,3 +1340,3 @@ `.parseAsync(data:unknown): Promise<T>` | ||
## `.default` | ||
### `.default` | ||
@@ -1411,2 +1413,4 @@ You can use transformers to implement the concept of "default values" in Zod. | ||
#### Error formatting | ||
You can use the `.format()` method to convert this error into a nested object. | ||
@@ -1421,3 +1425,3 @@ | ||
For detailed information about the possible error codes and how to customize error messages, check out the dedicated error handling guide: [ERROR_HANDLING.md](./ERROR_HANDLING.md) | ||
For detailed information about the possible error codes and how to customize error messages, check out the dedicated error handling guide: [ERROR_HANDLING.md](ERROR_HANDLING.md) | ||
@@ -1581,2 +1585,2 @@ # Comparison | ||
View the changelog at [CHANGELOG.md](https://github.com/colinhacks/zod/blob/master/CHANGELOG.md) | ||
View the changelog at [CHANGELOG.md](CHANGELOG.md) |
513873
1571