zod 3.16.1 → 3.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +37 -15
- package/lib/index.mjs +12 -1
- package/lib/index.umd.js +12 -1
- package/lib/types.d.ts +6 -2
- package/lib/types.js +12 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -47,6 +47,8 @@
|
|
|
47
47
|
- [Sponsors](#sponsors)
|
|
48
48
|
- [Ecosystem](#ecosystem)
|
|
49
49
|
- [Installation](#installation)
|
|
50
|
+
- [Node](#node)
|
|
51
|
+
- [Deno](#deno)
|
|
50
52
|
- [Basic usage](#basic-usage)
|
|
51
53
|
- [Primitives](#primitives)
|
|
52
54
|
- [Literals](#literals)
|
|
@@ -288,30 +290,47 @@ There are a growing number of tools that are built atop or support Zod natively!
|
|
|
288
290
|
|
|
289
291
|
## Installation
|
|
290
292
|
|
|
293
|
+
### Requirements
|
|
294
|
+
|
|
295
|
+
- TypeScript 4.1+!
|
|
296
|
+
- You must enable `strict` mode in your `tsconfig.json`. This is a best practice for all TypeScript projects.
|
|
297
|
+
|
|
298
|
+
```ts
|
|
299
|
+
// tsconfig.json
|
|
300
|
+
{
|
|
301
|
+
// ...
|
|
302
|
+
"compilerOptions": {
|
|
303
|
+
// ...
|
|
304
|
+
"strict": true
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
### Node/NPM
|
|
310
|
+
|
|
291
311
|
To install Zod v3:
|
|
292
312
|
|
|
293
313
|
```sh
|
|
294
|
-
npm install zod
|
|
314
|
+
npm install zod # npm
|
|
315
|
+
yarn add zod # yarn
|
|
316
|
+
pnpm add zod # pnpm
|
|
295
317
|
```
|
|
296
318
|
|
|
297
|
-
|
|
319
|
+
### Deno
|
|
320
|
+
|
|
321
|
+
Unlike Node, Deno relies on direct URL imports instead of a package manager like NPM. Zod is available on [deno.land/x](deno.land/x). The latest version can be imported like so:
|
|
298
322
|
|
|
299
323
|
```ts
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
}
|
|
324
|
+
import { z } from "https://deno.land/x/zod/mod.ts";
|
|
325
|
+
```
|
|
326
|
+
|
|
327
|
+
You can also specify a particular version:
|
|
328
|
+
|
|
329
|
+
```ts
|
|
330
|
+
import { z } from from "https://deno.land/x/zod@v3.16.1/mod.ts"
|
|
308
331
|
```
|
|
309
332
|
|
|
310
|
-
>
|
|
311
|
-
>
|
|
312
|
-
> - Zod 3.x requires TypeScript 4.1+
|
|
313
|
-
> - Zod 2.x requires TypeScript 3.7+
|
|
314
|
-
> - Zod 1.x requires TypeScript 3.3+
|
|
333
|
+
> The rest of this README assumes you are using NPM and importing directly from the `"zod"` package.
|
|
315
334
|
|
|
316
335
|
## Basic usage
|
|
317
336
|
|
|
@@ -402,6 +421,9 @@ z.string().uuid();
|
|
|
402
421
|
z.string().cuid();
|
|
403
422
|
z.string().regex(regex);
|
|
404
423
|
|
|
424
|
+
// trim whitespace
|
|
425
|
+
z.string().trim();
|
|
426
|
+
|
|
405
427
|
// deprecated, equivalent to .min(1)
|
|
406
428
|
z.string().nonempty();
|
|
407
429
|
|
package/lib/index.mjs
CHANGED
|
@@ -713,6 +713,10 @@ class ZodString extends ZodType {
|
|
|
713
713
|
* @see {@link ZodString.min}
|
|
714
714
|
*/
|
|
715
715
|
this.nonempty = (message) => this.min(1, errorUtil.errToObj(message));
|
|
716
|
+
this.trim = () => new ZodString({
|
|
717
|
+
...this._def,
|
|
718
|
+
checks: [...this._def.checks, { kind: "trim" }],
|
|
719
|
+
});
|
|
716
720
|
}
|
|
717
721
|
_parse(input) {
|
|
718
722
|
const parsedType = this._getType(input);
|
|
@@ -816,6 +820,12 @@ class ZodString extends ZodType {
|
|
|
816
820
|
status.dirty();
|
|
817
821
|
}
|
|
818
822
|
}
|
|
823
|
+
else if (check.kind === "trim") {
|
|
824
|
+
input.data = input.data.trim();
|
|
825
|
+
}
|
|
826
|
+
else {
|
|
827
|
+
util.assertNever(check);
|
|
828
|
+
}
|
|
819
829
|
}
|
|
820
830
|
return { status: status.value, value: input.data };
|
|
821
831
|
}
|
|
@@ -2382,10 +2392,11 @@ ZodLiteral.create = (value, params) => {
|
|
|
2382
2392
|
...processCreateParams(params),
|
|
2383
2393
|
});
|
|
2384
2394
|
};
|
|
2385
|
-
function createZodEnum(values) {
|
|
2395
|
+
function createZodEnum(values, params) {
|
|
2386
2396
|
return new ZodEnum({
|
|
2387
2397
|
values: values,
|
|
2388
2398
|
typeName: ZodFirstPartyTypeKind.ZodEnum,
|
|
2399
|
+
...processCreateParams(params),
|
|
2389
2400
|
});
|
|
2390
2401
|
}
|
|
2391
2402
|
class ZodEnum extends ZodType {
|
package/lib/index.umd.js
CHANGED
|
@@ -719,6 +719,10 @@
|
|
|
719
719
|
* @see {@link ZodString.min}
|
|
720
720
|
*/
|
|
721
721
|
this.nonempty = (message) => this.min(1, errorUtil.errToObj(message));
|
|
722
|
+
this.trim = () => new ZodString({
|
|
723
|
+
...this._def,
|
|
724
|
+
checks: [...this._def.checks, { kind: "trim" }],
|
|
725
|
+
});
|
|
722
726
|
}
|
|
723
727
|
_parse(input) {
|
|
724
728
|
const parsedType = this._getType(input);
|
|
@@ -822,6 +826,12 @@
|
|
|
822
826
|
status.dirty();
|
|
823
827
|
}
|
|
824
828
|
}
|
|
829
|
+
else if (check.kind === "trim") {
|
|
830
|
+
input.data = input.data.trim();
|
|
831
|
+
}
|
|
832
|
+
else {
|
|
833
|
+
util.assertNever(check);
|
|
834
|
+
}
|
|
825
835
|
}
|
|
826
836
|
return { status: status.value, value: input.data };
|
|
827
837
|
}
|
|
@@ -2388,10 +2398,11 @@
|
|
|
2388
2398
|
...processCreateParams(params),
|
|
2389
2399
|
});
|
|
2390
2400
|
};
|
|
2391
|
-
function createZodEnum(values) {
|
|
2401
|
+
function createZodEnum(values, params) {
|
|
2392
2402
|
return new ZodEnum({
|
|
2393
2403
|
values: values,
|
|
2394
2404
|
typeName: exports.ZodFirstPartyTypeKind.ZodEnum,
|
|
2405
|
+
...processCreateParams(params),
|
|
2395
2406
|
});
|
|
2396
2407
|
}
|
|
2397
2408
|
class ZodEnum extends ZodType {
|
package/lib/types.d.ts
CHANGED
|
@@ -102,6 +102,9 @@ declare type ZodStringCheck = {
|
|
|
102
102
|
kind: "regex";
|
|
103
103
|
regex: RegExp;
|
|
104
104
|
message?: string;
|
|
105
|
+
} | {
|
|
106
|
+
kind: "trim";
|
|
107
|
+
message?: string;
|
|
105
108
|
};
|
|
106
109
|
export interface ZodStringDef extends ZodTypeDef {
|
|
107
110
|
checks: ZodStringCheck[];
|
|
@@ -124,6 +127,7 @@ export declare class ZodString extends ZodType<string, ZodStringDef> {
|
|
|
124
127
|
* @see {@link ZodString.min}
|
|
125
128
|
*/
|
|
126
129
|
nonempty: (message?: errorUtil.ErrMessage | undefined) => ZodString;
|
|
130
|
+
trim: () => ZodString;
|
|
127
131
|
get isEmail(): boolean;
|
|
128
132
|
get isURL(): boolean;
|
|
129
133
|
get isUUID(): boolean;
|
|
@@ -530,8 +534,8 @@ export interface ZodEnumDef<T extends EnumValues = EnumValues> extends ZodTypeDe
|
|
|
530
534
|
declare type Writeable<T> = {
|
|
531
535
|
-readonly [P in keyof T]: T[P];
|
|
532
536
|
};
|
|
533
|
-
declare function createZodEnum<U extends string, T extends Readonly<[U, ...U[]]>>(values: T): ZodEnum<Writeable<T>>;
|
|
534
|
-
declare function createZodEnum<U extends string, T extends [U, ...U[]]>(values: T): ZodEnum<T>;
|
|
537
|
+
declare function createZodEnum<U extends string, T extends Readonly<[U, ...U[]]>>(values: T, params?: RawCreateParams): ZodEnum<Writeable<T>>;
|
|
538
|
+
declare function createZodEnum<U extends string, T extends [U, ...U[]]>(values: T, params?: RawCreateParams): ZodEnum<T>;
|
|
535
539
|
export declare class ZodEnum<T extends [string, ...string[]]> extends ZodType<T[number], ZodEnumDef<T>> {
|
|
536
540
|
_parse(input: ParseInput): ParseReturnType<this["_output"]>;
|
|
537
541
|
get options(): T;
|
package/lib/types.js
CHANGED
|
@@ -295,6 +295,10 @@ class ZodString extends ZodType {
|
|
|
295
295
|
* @see {@link ZodString.min}
|
|
296
296
|
*/
|
|
297
297
|
this.nonempty = (message) => this.min(1, errorUtil_1.errorUtil.errToObj(message));
|
|
298
|
+
this.trim = () => new ZodString({
|
|
299
|
+
...this._def,
|
|
300
|
+
checks: [...this._def.checks, { kind: "trim" }],
|
|
301
|
+
});
|
|
298
302
|
}
|
|
299
303
|
_parse(input) {
|
|
300
304
|
const parsedType = this._getType(input);
|
|
@@ -398,6 +402,12 @@ class ZodString extends ZodType {
|
|
|
398
402
|
status.dirty();
|
|
399
403
|
}
|
|
400
404
|
}
|
|
405
|
+
else if (check.kind === "trim") {
|
|
406
|
+
input.data = input.data.trim();
|
|
407
|
+
}
|
|
408
|
+
else {
|
|
409
|
+
util_1.util.assertNever(check);
|
|
410
|
+
}
|
|
401
411
|
}
|
|
402
412
|
return { status: status.value, value: input.data };
|
|
403
413
|
}
|
|
@@ -1988,10 +1998,11 @@ ZodLiteral.create = (value, params) => {
|
|
|
1988
1998
|
...processCreateParams(params),
|
|
1989
1999
|
});
|
|
1990
2000
|
};
|
|
1991
|
-
function createZodEnum(values) {
|
|
2001
|
+
function createZodEnum(values, params) {
|
|
1992
2002
|
return new ZodEnum({
|
|
1993
2003
|
values: values,
|
|
1994
2004
|
typeName: ZodFirstPartyTypeKind.ZodEnum,
|
|
2005
|
+
...processCreateParams(params),
|
|
1995
2006
|
});
|
|
1996
2007
|
}
|
|
1997
2008
|
class ZodEnum extends ZodType {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "zod",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.17.0",
|
|
4
4
|
"description": "TypeScript-first schema declaration and validation library with static type inference",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"types": "./index.d.ts",
|
|
@@ -51,7 +51,7 @@
|
|
|
51
51
|
"fix": "yarn lint:fix && yarn prettier:fix",
|
|
52
52
|
"clean": "rm -rf lib/* deno/lib/*",
|
|
53
53
|
"build": "yarn run clean && npm run build:cjs && npm run build:esm && npm run build:deno",
|
|
54
|
-
"build:deno": "node ./deno/build.mjs",
|
|
54
|
+
"build:deno": "node ./deno/build.mjs && cp ./README.md ./deno/lib",
|
|
55
55
|
"build:esm": "rollup --config rollup.config.js",
|
|
56
56
|
"build:cjs": "tsc --p tsconfig.cjs.json",
|
|
57
57
|
"build:types": "tsc --p tsconfig.types.json",
|