Skip to content

Commit

Permalink
Merge branch 'master' into template-literal
Browse files Browse the repository at this point in the history
  • Loading branch information
igalklebanov authored Oct 27, 2023
2 parents e2ba25f + 481c9ba commit a8e9afd
Show file tree
Hide file tree
Showing 17 changed files with 849 additions and 171 deletions.
17 changes: 0 additions & 17 deletions .github/stale.yml

This file was deleted.

1 change: 0 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ jobs:
- run: yarn build
- run: yarn test


test-deno:
runs-on: ubuntu-latest
strategy:
Expand Down
2 changes: 1 addition & 1 deletion ERROR_HANDLING.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Each ZodError has an `issues` property that is an array of `ZodIssues`. Each iss

## ZodIssue

`ZodIssue` is _not_ a class. It is a [discriminated union](https://www.typescriptlang.org/docs/handbook/advanced-types.html#discriminated-unions).
`ZodIssue` is _not_ a class. It is a [discriminated union](https://www.typescriptlang.org/docs/handbook/2/narrowing.html#discriminated-unions).

The link above is the best way to learn about the concept. Discriminated unions are an ideal way to represent a data structures that may be one of many possible variants. You can see all the possible variants defined [here](./src/ZodError.ts). They are also described in the table below if you prefer.

Expand Down
115 changes: 68 additions & 47 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@
- [`.or`](#or)
- [`.and`](#and)
- [`.brand`](#brand)
- [`.pipe()`](#pipe)
- [`.readonly`](#readonly)
- [`.pipe`](#pipe)
- [You can use `.pipe()` to fix common issues with `z.coerce`.](#you-can-use-pipe-to-fix-common-issues-with-zcoerce)
- [Guides and concepts](#guides-and-concepts)
- [Type inference](#type-inference)
Expand Down Expand Up @@ -185,23 +186,19 @@ Sponsorship at any level is appreciated and encouraged. For individual developer
<table>
<tr>
<td align="center">
<a href="https://astro.build/">
<img src="https://avatars.githubusercontent.com/u/44914786?s=200&v=4" width="200px;" alt="Astro" />
<a href="https://speakeasyapi.dev/">
<img src="https://avatars.githubusercontent.com/u/91446104?s=200&v=4" width="200px;" alt="Speakeasy API" />
</a>
<br />
<b>Astro</b>
<b>Speakeasy</b>
<br />
<a href="https://astro.build">astro.build</a>
<a href="https://speakeasyapi.dev/">speakeasyapi.dev</a>
<br />
<p width="200px">
Astro is a new kind of static <br/>
site builder for the modern web. <br/>
Powerful developer experience meets <br/>
lightweight output.</p>
<p width="200px">SDKs, Terraform, Docs.<br/>Your API made enterprise-ready.</p>
</td>
<td align="center">
<a href="https://glow.app/">
<img src="https://i.imgur.com/R0R43S2.jpg" width="200px;" alt="" />
<img src="https://i.imgur.com/R0R43S2.jpg" width="200px;" alt="Glow Wallet" />
</a>
<br />
<b>Glow Wallet</b>
Expand All @@ -223,17 +220,6 @@ Sponsorship at any level is appreciated and encouraged. For individual developer
<br />
<a href="https://deletype.com">deletype.com</a>
</td>
<td align="center">
<a href="https://proxy.com/">
<img src="https://avatars.githubusercontent.com/u/14321439?s=200&v=4" width="200px;" alt="Proxy logo" />
</a>
<br />
<b>Proxy</b>
<br />
<a href="https://proxy.com">proxy.com</a>
</td>
</tr>
<tr>
<td align="center">
<a href="https://trigger.dev/">
<img src="https://avatars.githubusercontent.com/u/95297378?s=200&v=4" width="200px;" alt="Trigger.dev logo" />
Expand All @@ -245,19 +231,19 @@ Sponsorship at any level is appreciated and encouraged. For individual developer
<br/>
<p>Effortless automation for developers.</p>
</td>
</tr>
<tr>
<td align="center">
<a href="https://transloadit.com/">
<a href="https://transloadit.com/?utm_source=zod&utm_medium=referral&utm_campaign=sponsorship&utm_content=github">
<img src="https://avatars.githubusercontent.com/u/125754?s=200&v=4" width="200px;" alt="Transloadit logo" />
</a>
<br />
<b>Transloadit</b>
<br />
<a href="https://transloadit.com">transloadit.com</a>
<a href="https://transloadit.com/?utm_source=zod&utm_medium=referral&utm_campaign=sponsorship&utm_content=github">transloadit.com</a>
<br/>
<p>Simple file processing for developers.</p>
</td>
</tr>
<tr>
<td align="center">
<a href="https://infisical.com">
<img src="https://avatars.githubusercontent.com/u/107880645?s=200&v=4" width="200px;" alt="Infisical logo" />
Expand All @@ -270,13 +256,25 @@ Sponsorship at any level is appreciated and encouraged. For individual developer
<p>Open-source platform for secret<br/>management: sync secrets across your<br/>team/infrastructure and prevent secret leaks.</p>
</td>
</tr>
<tr>
<td align="center">
<a href="https://whop.com/">
<img src="https://avatars.githubusercontent.com/u/91036480?s=200&v=4" width="200px;" alt="Whop logo" />
</a>
<br />
<b>Whop</b>
<br />
<a href="https://whop.com/">whop.com</a>
<br />
<p width="200px">A marketplace for really cool internet products.</p>
</td>
</table>

#### Silver

<table>
<tr>
<td align="center" colspan="2">
<td align="center" colspan="2">
<a href="https://www.numeric.io">
<img src="https://i.imgur.com/kTiLtZt.png" width="250px;" alt="Numeric logo" />
</a>
Expand All @@ -286,17 +284,6 @@ Sponsorship at any level is appreciated and encouraged. For individual developer
<a href="https://www.numeric.io">numeric.io</a>
</td>
<td align="center">
<a href="https://snaplet.dev">
<img src="https://avatars.githubusercontent.com/u/69029941?s=200&v=4" width="150px;" alt="Snaplet logo" />
</a>
<br />
<b>Snaplet</b>
<br />
<a href="https://snaplet.dev">snaplet.dev</a>
</td>
</tr>
<tr>
<td align="center">
<a href="https://marcatopartners.com/">
<img src="https://avatars.githubusercontent.com/u/84106192?s=200&v=4" width="150px;" alt="Marcato Partners" />
</a>
Expand All @@ -305,7 +292,9 @@ Sponsorship at any level is appreciated and encouraged. For individual developer
<br />
<a href="https://marcatopartners.com/">marcatopartners.com</a>
</td>
<td align="center">
</tr>
<tr>
<td align="center">
<a href="https://interval.com">
<img src="https://avatars.githubusercontent.com/u/67802063?s=200&v=4" width="150px;" alt="" />
</a>
Expand All @@ -323,8 +312,6 @@ Sponsorship at any level is appreciated and encouraged. For individual developer
<br />
<a href="https://seasoned.cc">seasoned.cc</a>
</td>
</tr>
<tr>
<td align="center">
<a href="https://www.bamboocreative.nz/">
<img src="https://avatars.githubusercontent.com/u/41406870?v=4" width="150px;" alt="Bamboo Creative logo" />
Expand Down Expand Up @@ -488,6 +475,7 @@ There are a growing number of tools that are built atop or support Zod natively!
- [`nestjs-graphql-zod`](https://github.com/incetarik/nestjs-graphql-zod): Generates NestJS GraphQL model classes from Zod schemas. Provides GraphQL method decorators working with Zod schemas.
- [`zod-openapi`](https://github.com/samchungy/zod-openapi): Create full OpenAPI v3.x documentation from Zod schemas.
- [`fastify-zod-openapi`](https://github.com/samchungy/fastify-zod-openapi): Fastify type provider, validation, serialization and @fastify/swagger support for Zod schemas.
- [`typeschema`](https://typeschema.com/): Universal adapter for schema validation.

#### X to Zod

Expand All @@ -501,6 +489,8 @@ There are a growing number of tools that are built atop or support Zod natively!
- [`prisma-zod-generator`](https://github.com/omar-dulaimi/prisma-zod-generator): Emit Zod schemas from your Prisma schema.
- [`prisma-trpc-generator`](https://github.com/omar-dulaimi/prisma-trpc-generator): Emit fully implemented tRPC routers and their validation schemas using Zod.
- [`zod-prisma-types`](https://github.com/chrishoermann/zod-prisma-types) Create Zod types from your Prisma models.
- [`quicktype`](https://app.quicktype.io/): Convert JSON objects and JSON schemas into Zod schemas.
- [`@sanity-typed/zod`](https://github.com/saiichihashimoto/sanity-typed/tree/main/packages/zod): Generate Zod Schemas from [Sanity Schemas](https://www.sanity.io/docs/schema-types).

#### Mocking

Expand All @@ -521,6 +511,7 @@ There are a growing number of tools that are built atop or support Zod natively!
#### Utilities for Zod

- [`zod_utilz`](https://github.com/JacobWeisenburger/zod_utilz): Framework agnostic utilities for Zod.
- [`zod-sandbox`](https://github.com/nereumelo/zod-sandbox): Controlled environment for testing zod schemas. [Live demo](https://zod-sandbox.vercel.app/).

## Installation

Expand Down Expand Up @@ -719,7 +710,7 @@ z.string().regex(regex);
z.string().includes(string);
z.string().startsWith(string);
z.string().endsWith(string);
z.string().datetime(); // defaults to UTC, see below for options
z.string().datetime(); // ISO 8601; default is without UTC offset, see below for options
z.string().ip(); // defaults to IPv4 and IPv6, see below for options

// transformations
Expand Down Expand Up @@ -758,7 +749,7 @@ z.string().ip({ message: "Invalid IP address" });
### ISO datetimes
The `z.string().datetime()` method defaults to UTC validation: no timezone offsets with arbitrary sub-second decimal precision.
The `z.string().datetime()` method enforces ISO 8601; default is no timezone offsets and arbitrary sub-second decimal precision.
```ts
const datetime = z.string().datetime();
Expand Down Expand Up @@ -2011,7 +2002,7 @@ You can create a Zod schema for any TypeScript type by using `z.custom()`. This

```ts
const px = z.custom<`${number}px`>((val) => {
return /^\d+px$/.test(val as string);
return typeof val === "string" ? /^\d+px$/.test(val) : false;
});

type px = z.infer<typeof px>; // `${number}px`
Expand Down Expand Up @@ -2574,7 +2565,38 @@ type Cat = z.infer<typeof Cat>;

Note that branded types do not affect the runtime result of `.parse`. It is a static-only construct.

### `.pipe()`
### `.readonly`

`.readonly() => ZodReadonly<this>`

This method returns a `ZodReadonly` schema instance that parses the input using the base schema, then calls `Object.freeze()` on the result. The inferred type is also marked as `readonly`.

```ts
const schema = z.object({ name: string }).readonly();
type schema = z.infer<typeof schema>;
// Readonly<{name: string}>

const result = schema.parse({ name: "fido" });
result.name = "simba"; // error
```

The inferred type uses TypeScript's built-in readonly types when relevant.

```ts
z.array(z.string()).readonly();
// readonly string[]

z.tuple([z.string(), z.number()]).readonly();
// readonly [string, number]

z.map(z.string(), z.date()).readonly();
// ReadonlyMap<string, Date>

z.set(z.string()).readonly();
// ReadonlySet<Promise<string>>
```

### `.pipe`

Schemas can be chained into validation "pipelines". It's useful for easily validating the result after a `.transform()`:

Expand Down Expand Up @@ -2912,10 +2934,9 @@ This more declarative API makes schema definitions vastly more concise.

[https://github.com/pelotom/runtypes](https://github.com/pelotom/runtypes)

Good type inference support. They DO support readonly types, which Zod does not.
Good type inference support.

- Supports "pattern matching": computed properties that distribute over unions
- Supports readonly types
- Missing object methods: (deepPartial, merge)
- Missing nonempty arrays with proper typing (`[T, ...T[]]`)
- Missing promise schemas
Expand Down
3 changes: 1 addition & 2 deletions configs/tsconfig.cjs.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
"outDir": "../lib",
"declaration": true,
"declarationMap": false,
"sourceMap": false,
"removeComments": true
"sourceMap": false
},
"exclude": ["../src/**/__tests__", "../playground.ts"]
}
Loading

0 comments on commit a8e9afd

Please sign in to comment.