Skip to content

Commit

Permalink
docs: fix typo (#1699)
Browse files Browse the repository at this point in the history
* Update README.md

* simplify example for `.parseAsync`

* refine comments

* use a more reasonable condition

* fix a punctuation

* Update README.md

* change variable name
  • Loading branch information
zetaraku committed Dec 24, 2022
1 parent 827cb5d commit dbb437b
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1682,7 +1682,7 @@ Given any Zod schema, you can call its `.parse` method to check `data` is valid.
const stringSchema = z.string();

stringSchema.parse("fish"); // => returns "fish"
stringSchema.parse(12); // throws Error('Non-string type: number');
stringSchema.parse(12); // throws error
```

### `.parseAsync`
Expand All @@ -1692,11 +1692,10 @@ stringSchema.parse(12); // throws Error('Non-string type: number');
If you use asynchronous [refinements](#refine) or [transforms](#transform) (more on those later), you'll need to use `.parseAsync`

```ts
const stringSchema1 = z.string().refine(async (val) => val.length < 20);
const value1 = await stringSchema.parseAsync("hello"); // => hello
const stringSchema = z.string().refine(async (val) => val.length <= 8);

const stringSchema2 = z.string().refine(async (val) => val.length > 20);
const value2 = await stringSchema.parseAsync("hello"); // => throws
await stringSchema.parseAsync("hello"); // => returns "hello"
await stringSchema.parseAsync("hello world"); // => throws error
```

### `.safeParse`
Expand Down Expand Up @@ -1781,7 +1780,7 @@ type RefineParams = {
};
```

For advanced cases, the second argument can also be a function that returns `RefineParams`/
For advanced cases, the second argument can also be a function that returns `RefineParams`.

```ts
const longString = z.string().refine(
Expand Down Expand Up @@ -1971,12 +1970,12 @@ emailToDomain.parse("colinhacks@example.com"); // => example.com

#### Validating during transform

The `.transform` method can simultaneously validate and transform the value. This is often simpler and less duplicative than chaining `refine` and `validate`.
The `.transform` method can simultaneously validate and transform the value. This is often simpler and less duplicative than chaining `transform` and `refine`.

As with `.superRefine`, the transform function receives a `ctx` object with a `addIssue` method that can be used to register validation issues.

```ts
const Strings = z.string().transform((val, ctx) => {
const numberInString = z.string().transform((val, ctx) => {
const parsed = parseInt(val);
if (isNaN(parsed)) {
ctx.addIssue({
Expand Down

0 comments on commit dbb437b

Please sign in to comment.