Skip to content

Commit

Permalink
Merge branch 'main' of github.com:wataryooou/zod into ip-range-valida…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
wataryooou committed Dec 14, 2024
2 parents 45428a3 + f7ad261 commit deb9393
Show file tree
Hide file tree
Showing 23 changed files with 1,451 additions and 299 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
strategy:
matrix:
node: ["latest"]
typescript: ["4.5", "4.6", "4.7", "4.8", "4.9", "5.0", "5.3"]
typescript: ["5.0", "latest"]
name: Test with TypeScript ${{ matrix.typescript }} on Node ${{ matrix.node }}
steps:
- uses: actions/checkout@v4
Expand Down
10 changes: 6 additions & 4 deletions ERROR_HANDLING.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,12 @@ Here's a sample Person schema.
```ts
const person = z.object({
names: z.array(z.string()).nonempty(), // at least 1 name
address: z.object({
line1: z.string(),
zipCode: z.number().min(10000), // American 5-digit code
}).strict(); // do not allow unrecognized keys
address: z
.object({
line1: z.string(),
zipCode: z.number().min(10000), // American 5-digit code
})
.strict(), // do not allow unrecognized keys
});
```

Expand Down
7 changes: 7 additions & 0 deletions FUNDING.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"drips": {
"ethereum": {
"ownedBy": "0xAe9ae688557471b0317734a54bE095b3C675DA2f"
}
}
}
76 changes: 57 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@
- [Utilities for Zod](#utilities-for-zod)
- [Installation](#installation)
- [Requirements](#requirements)
- [From `npm` (Node/Bun)](#from-npm-nodebun)
- [From `deno.land/x` (Deno)](#from-denolandx-deno)
- [From `npm`](#from-npm)
- [Basic usage](#basic-usage)
- [Primitives](#primitives)
- [Coercion for primitives](#coercion-for-primitives)
Expand All @@ -77,11 +76,12 @@
- [Times](#times)
- [IP addresses](#ip-addresses)
- [IP addresses range](#ip-addresses-range)
- [IP ranges](#ip-ranges-cidr)
- [Numbers](#numbers)
- [BigInts](#bigints)
- [NaNs](#nans)
- [Booleans](#booleans)
- [Dates](#dates)
- [Dates](#dates-1)
- [Zod enums](#zod-enums)
- [Native enums](#native-enums)
- [Optionals](#optionals)
Expand Down Expand Up @@ -213,6 +213,24 @@ Sponsorship at any level is appreciated and encouraged. If you built a paid prod
<h3 align="center">Platinum</h3>

<table align="center" style="justify-content: center;align-items: center;display: flex;">
<tr>
<td align="center">
<p></p>
<p>
<a href="https://liblab.com/?utm_source=zod">
<picture height="62px">
<source media="(prefers-color-scheme: dark)" srcset="https://github.com/user-attachments/assets/34dfa1a2-ce94-46f4-8902-fbfac3e1a9bc">
<img alt="LibLab" height="62px" src="https://github.com/user-attachments/assets/3de0b617-5137-49c4-b72d-a033cbe602d8">
</picture>
</a>
<br />
Generate better SDKs for your APIs
<br/>
<a href="https://liblab.com/?utm_source=zod" style="text-decoration:none;">liblab.com</a>
</p>
<p></p>
</td>
</tr>
<tr>
<td align="center">
<p></p>
Expand Down Expand Up @@ -475,6 +493,7 @@ There are a growing number of tools that are built atop or support Zod natively!
- [`tapiduck`](https://github.com/sumukhbarve/monoduck/blob/main/src/tapiduck/README.md): End-to-end typesafe JSON APIs with Zod and Express; a bit like tRPC, but simpler.
- [`koa-zod-router`](https://github.com/JakeFenley/koa-zod-router): Create typesafe routes in Koa with I/O validation using Zod.
- [`zod-sockets`](https://github.com/RobinTail/zod-sockets): Zod-powered Socket.IO microframework with I/O validation and built-in AsyncAPI specs
- [`oas-tszod-gen`](https://github.com/inkognitro/oas-tszod-gen): Client SDK code generator to convert OpenApi v3 specifications into TS endpoint caller functions with Zod types.

#### Form integrations

Expand All @@ -493,6 +512,8 @@ There are a growing number of tools that are built atop or support Zod natively!
- [`mobx-zod-form`](https://github.com/MonoidDev/mobx-zod-form): Data-first form builder based on MobX & Zod.
- [`@vee-validate/zod`](https://github.com/logaretm/vee-validate/tree/main/packages/zod): Form library for Vue.js with Zod schema validation.
- [`zod-form-renderer`](https://github.com/thepeaklab/zod-form-renderer): Auto-infer form fields from zod schema and render them with react-hook-form with E2E type safety.
- [`antd-zod`](https://github.com/MrBr/antd-zod): Zod adapter for Ant Design form fields validation.
- [`frrm`](https://github.com/schalkventer/frrm): Tiny 0.5kb Zod-based, HTML form abstraction that goes brr.

#### Zod to X

Expand Down Expand Up @@ -575,10 +596,11 @@ There are a growing number of tools that are built atop or support Zod natively!
}
```

### From `npm` (Node/Bun)
### From `npm`

```sh
npm install zod # npm
deno add npm:zod # deno
yarn add zod # yarn
bun add zod # bun
pnpm add zod # pnpm
Expand All @@ -588,25 +610,12 @@ Zod also publishes a canary version on every commit. To install the canary:

```sh
npm install zod@canary # npm
deno add npm:zod@canary # deno
yarn add zod@canary # yarn
bun add zod@canary # bun
pnpm add zod@canary # pnpm
```

### From `deno.land/x` (Deno)

Unlike Node, Deno relies on direct URL imports instead of a package manager like NPM. Zod is available on [deno.land/x](https://deno.land/x). The latest version can be imported like so:

```ts
import { z } from "https://deno.land/x/zod/mod.ts";
```

You can also specify a particular version:

```ts
import { z } from "https://deno.land/x/[email protected]/mod.ts";
```

> The rest of this README assumes you are using npm and importing directly from the `"zod"` package.
## Basic usage
Expand Down Expand Up @@ -770,6 +779,7 @@ z.string().startsWith(string);
z.string().endsWith(string);
z.string().datetime(); // ISO 8601; by default only `Z` timezone allowed
z.string().ip(); // defaults to allow both IPv4 and IPv6
z.string().cidr(); // defaults to allow both IPv4 and IPv6

// transforms
z.string().trim(); // trim whitespace
Expand Down Expand Up @@ -811,6 +821,7 @@ z.string().datetime({ message: "Invalid datetime string! Must be UTC." });
z.string().date({ message: "Invalid date string!" });
z.string().time({ message: "Invalid time string!" });
z.string().ip({ message: "Invalid IP address" });
z.string().cidr({ message: "Invalid CIDR" });
```

### Datetimes
Expand Down Expand Up @@ -840,6 +851,13 @@ datetime.parse("2020-01-01T00:00:00.123+02"); // pass (only offset hours)
datetime.parse("2020-01-01T00:00:00Z"); // pass (Z still supported)
```

Allow unqualified (timezone-less) datetimes with the `local` flag.

```ts
const schema = z.string().datetime({ local: true });
schema.parse("2020-01-01T00:00:00"); // pass
```

You can additionally constrain the allowable `precision`. By default, arbitrary sub-second precision is supported (but optional).

```ts
Expand Down Expand Up @@ -893,7 +911,7 @@ time.parse("00:00:00"); // fail

### IP addresses

The `z.string().ip()` method by default validate IPv4 and IPv6.
By default `.ip()` allows both IPv4 and IPv6.

```ts
const ip = z.string().ip();
Expand Down Expand Up @@ -933,6 +951,26 @@ ipv6Range.parse("2001:db8::1"); // pass
ipv6Range.parse("2001:db9::1"); // fail
```

### IP ranges (CIDR)

Validate IP address ranges specified with [CIDR notation](https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing). By default, `.cidr()` allows both IPv4 and IPv6.

```ts
const cidr = z.string().cidr();
cidr.parse("192.168.0.0/24"); // pass
cidr.parse("2001:db8::/32"); // pass
```

You can specify a version with the `version` parameter.

```ts
const ipv4Cidr = z.string().cidr({ version: "v4" });
ipv4Cidr.parse("84d5:51a0:9114:1855:4cfa:f2d7:1f12:7003"); // fail

const ipv6Cidr = z.string().cidr({ version: "v6" });
ipv6Cidr.parse("192.168.1.1"); // fail
```

## Numbers

You can customize certain error messages when creating a number schema.
Expand Down
17 changes: 2 additions & 15 deletions README_ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -316,29 +316,16 @@ _要在这里看到你的名字 + Twitter + 網站 , 请在[Freelancer](https://
}
```

### `npm`(Node/Bun)安装
### `npm` 安装

```sh
npm install zod
deno add npm:zod # deno
yarn add zod # yarn
bun add zod # bun
pnpm add zod # pnpm
```

### `deno.land/x` (Deno)安装

和 Node 不同,Deno 依靠一个直接的 URL 导入而非像 npm 这样的包管理器。可以这样导入最新版本的 Zod:

```ts
import { z } from "https://deno.land/x/zod/mod.ts";
```

你也可以指定一个具体的版本:

```ts
import { z } from "https://deno.land/x/[email protected]/mod.ts";
```

> README 的剩余部分假定你是直接通过 npm 安装的`zod`包。
# 基本用法
Expand Down
Loading

0 comments on commit deb9393

Please sign in to comment.