zod 3.14.2 → 3.14.5

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/lib/types.d.ts CHANGED
@@ -15,15 +15,7 @@ export declare type ZodTypeAny = ZodType<any, any, any>;
15
15
  export declare type TypeOf<T extends ZodType<any, any, any>> = T["_output"];
16
16
  export declare type input<T extends ZodType<any, any, any>> = T["_input"];
17
17
  export declare type output<T extends ZodType<any, any, any>> = T["_output"];
18
- declare type allKeys<T> = T extends any ? keyof T : never;
19
- export declare type TypeOfFlattenedError<T extends ZodType<any, any, any>, U = string> = {
20
- formErrors: U[];
21
- fieldErrors: {
22
- [P in allKeys<TypeOf<T>>]?: U[];
23
- };
24
- };
25
- export declare type TypeOfFormErrors<T extends ZodType<any, any, any>> = TypeOfFlattenedError<T>;
26
- export type { TypeOf as infer, TypeOfFlattenedError as inferFlattenedErrors, TypeOfFormErrors as inferFormErrors, };
18
+ export type { TypeOf as infer };
27
19
  export declare type CustomErrorParams = Partial<util.Omit<ZodCustomIssue, "code">>;
28
20
  export interface ZodTypeDef {
29
21
  errorMap?: ZodErrorMap;
@@ -44,7 +36,7 @@ export declare type SafeParseError<Input> = {
44
36
  error: ZodError<Input>;
45
37
  };
46
38
  export declare type SafeParseReturnType<Input, Output> = SafeParseSuccess<Output> | SafeParseError<Input>;
47
- export declare abstract class ZodType<Output, Def extends ZodTypeDef = ZodTypeDef, Input = Output> {
39
+ export declare abstract class ZodType<Output = any, Def extends ZodTypeDef = ZodTypeDef, Input = Output> {
48
40
  readonly _type: Output;
49
41
  readonly _output: Output;
50
42
  readonly _input: Input;
@@ -65,10 +57,6 @@ export declare abstract class ZodType<Output, Def extends ZodTypeDef = ZodTypeDe
65
57
  safeParseAsync(data: unknown, params?: Partial<ParseParams>): Promise<SafeParseReturnType<Input, Output>>;
66
58
  /** Alias of safeParseAsync */
67
59
  spa: (data: unknown, params?: Partial<ParseParams> | undefined) => Promise<SafeParseReturnType<Input, Output>>;
68
- /** The .is method has been removed in Zod 3. For details see https://github.com/colinhacks/zod/tree/v3. */
69
- is: never;
70
- /** The .check method has been removed in Zod 3. For details see https://github.com/colinhacks/zod/tree/v3. */
71
- check: never;
72
60
  refine<RefinedOutput extends Output>(check: (arg: Output) => arg is RefinedOutput, message?: string | CustomErrorParams | ((arg: Output) => CustomErrorParams)): ZodEffects<this, RefinedOutput, RefinedOutput>;
73
61
  refine(check: (arg: Output) => unknown | Promise<unknown>, message?: string | CustomErrorParams | ((arg: Output) => CustomErrorParams)): ZodEffects<this, Output, Input>;
74
62
  refinement<RefinedOutput extends Output>(check: (arg: Output) => arg is RefinedOutput, refinementData: IssueData | ((arg: Output, ctx: RefinementCtx) => IssueData)): ZodEffects<this, RefinedOutput, RefinedOutput>;
@@ -279,38 +267,30 @@ export declare class ZodArray<T extends ZodTypeAny, Cardinality extends ArrayCar
279
267
  }
280
268
  export declare type ZodNonEmptyArray<T extends ZodTypeAny> = ZodArray<T, "atleastone">;
281
269
  export declare namespace objectUtil {
282
- export type MergeShapes<U extends ZodRawShape, V extends ZodRawShape> = {
270
+ type MergeShapes<U extends ZodRawShape, V extends ZodRawShape> = {
283
271
  [k in Exclude<keyof U, keyof V>]: U[k];
284
272
  } & V;
285
- type optionalKeys<T extends object> = {
286
- [k in keyof T]: undefined extends T[k] ? k : never;
287
- }[keyof T];
288
273
  type requiredKeys<T extends object> = {
289
274
  [k in keyof T]: undefined extends T[k] ? never : k;
290
275
  }[keyof T];
291
- export type addQuestionMarks<T extends object> = {
292
- [k in optionalKeys<T>]?: T[k];
276
+ type addQuestionMarks<T extends object> = {
277
+ [k in keyof T]?: T[k];
293
278
  } & {
294
279
  [k in requiredKeys<T>]: T[k];
295
280
  };
296
- export type identity<T> = T;
297
- export type flatten<T extends object> = identity<{
281
+ type identity<T> = T;
282
+ type flatten<T extends object> = identity<{
298
283
  [k in keyof T]: T[k];
299
284
  }>;
300
- export type noNeverKeys<T extends ZodRawShape> = {
285
+ type noNeverKeys<T extends ZodRawShape> = {
301
286
  [k in keyof T]: [T[k]] extends [never] ? never : k;
302
287
  }[keyof T];
303
- export type noNever<T extends ZodRawShape> = identity<{
288
+ type noNever<T extends ZodRawShape> = identity<{
304
289
  [k in noNeverKeys<T>]: k extends keyof T ? T[k] : never;
305
290
  }>;
306
- export const mergeShapes: <U extends ZodRawShape, T extends ZodRawShape>(first: U, second: T) => T & U;
307
- export {};
291
+ const mergeShapes: <U extends ZodRawShape, T extends ZodRawShape>(first: U, second: T) => T & U;
308
292
  }
309
- export declare type extendShape<A, B> = {
310
- [k in Exclude<keyof A, keyof B>]: A[k];
311
- } & {
312
- [k in keyof B]: B[k];
313
- };
293
+ export declare type extendShape<A, B> = Omit<A, keyof B> & B;
314
294
  declare type UnknownKeysParam = "passthrough" | "strict" | "strip";
315
295
  export interface ZodObjectDef<T extends ZodRawShape = ZodRawShape, UnknownKeys extends UnknownKeysParam = UnknownKeysParam, Catchall extends ZodTypeAny = ZodTypeAny> extends ZodTypeDef {
316
296
  typeName: ZodFirstPartyTypeKind.ZodObject;
@@ -365,14 +345,10 @@ export declare class ZodObject<T extends ZodRawShape, UnknownKeys extends Unknow
365
345
  catchall<Index extends ZodTypeAny>(index: Index): ZodObject<T, UnknownKeys, Index>;
366
346
  pick<Mask extends {
367
347
  [k in keyof T]?: true;
368
- }>(mask: Mask): ZodObject<objectUtil.noNever<{
369
- [k in keyof Mask]: k extends keyof T ? T[k] : never;
370
- }>, UnknownKeys, Catchall>;
348
+ }>(mask: Mask): ZodObject<Pick<T, Extract<keyof T, keyof Mask>>, UnknownKeys, Catchall>;
371
349
  omit<Mask extends {
372
350
  [k in keyof T]?: true;
373
- }>(mask: Mask): ZodObject<objectUtil.noNever<{
374
- [k in keyof T]: k extends keyof Mask ? never : T[k];
375
- }>, UnknownKeys, Catchall>;
351
+ }>(mask: Mask): ZodObject<Omit<T, keyof Mask>, UnknownKeys, Catchall>;
376
352
  deepPartial(): partialUtil.DeepPartial<this>;
377
353
  partial(): ZodObject<{
378
354
  [k in keyof T]: ZodOptional<T[k]>;
package/lib/types.js CHANGED
@@ -6,6 +6,17 @@ const errorUtil_1 = require("./helpers/errorUtil");
6
6
  const parseUtil_1 = require("./helpers/parseUtil");
7
7
  const util_1 = require("./helpers/util");
8
8
  const ZodError_1 = require("./ZodError");
9
+ class ParseInputLazyPath {
10
+ constructor(parent, value, path, key) {
11
+ this.parent = parent;
12
+ this.data = value;
13
+ this._path = path;
14
+ this._key = key;
15
+ }
16
+ get path() {
17
+ return this._path.concat(this._key);
18
+ }
19
+ }
9
20
  const handleResult = (ctx, result) => {
10
21
  if ((0, parseUtil_1.isValid)(result)) {
11
22
  return { success: true, data: result.value };
@@ -62,8 +73,8 @@ class ZodType {
62
73
  this.transform = this.transform.bind(this);
63
74
  this.default = this.default.bind(this);
64
75
  this.describe = this.describe.bind(this);
65
- this.isOptional = this.isOptional.bind(this);
66
76
  this.isNullable = this.isNullable.bind(this);
77
+ this.isOptional = this.isOptional.bind(this);
67
78
  }
68
79
  get description() {
69
80
  return this._def.description;
@@ -117,7 +128,6 @@ class ZodType {
117
128
  common: {
118
129
  issues: [],
119
130
  async: (_a = params === null || params === void 0 ? void 0 : params.async) !== null && _a !== void 0 ? _a : false,
120
- typeCache: typeof Map !== "undefined" ? new Map() : undefined,
121
131
  contextualErrorMap: params === null || params === void 0 ? void 0 : params.errorMap,
122
132
  },
123
133
  path: (params === null || params === void 0 ? void 0 : params.path) || [],
@@ -141,7 +151,6 @@ class ZodType {
141
151
  issues: [],
142
152
  contextualErrorMap: params === null || params === void 0 ? void 0 : params.errorMap,
143
153
  async: true,
144
- typeCache: typeof Map !== "undefined" ? new Map() : undefined,
145
154
  },
146
155
  path: (params === null || params === void 0 ? void 0 : params.path) || [],
147
156
  schemaErrorMap: this._def.errorMap,
@@ -909,21 +918,13 @@ class ZodArray extends ZodType {
909
918
  }
910
919
  if (ctx.common.async) {
911
920
  return Promise.all(ctx.data.map((item, i) => {
912
- return def.type._parseAsync({
913
- parent: ctx,
914
- path: [...ctx.path, i],
915
- data: item,
916
- });
921
+ return def.type._parseAsync(new ParseInputLazyPath(ctx, item, ctx.path, i));
917
922
  })).then((result) => {
918
923
  return parseUtil_1.ParseStatus.mergeArray(status, result);
919
924
  });
920
925
  }
921
926
  const result = ctx.data.map((item, i) => {
922
- return def.type._parseSync({
923
- parent: ctx,
924
- path: [...ctx.path, i],
925
- data: item,
926
- });
927
+ return def.type._parseSync(new ParseInputLazyPath(ctx, item, ctx.path, i));
927
928
  });
928
929
  return parseUtil_1.ParseStatus.mergeArray(status, result);
929
930
  }
@@ -1044,19 +1045,19 @@ class ZodObject extends ZodType {
1044
1045
  }
1045
1046
  const { status, ctx } = this._processInputParams(input);
1046
1047
  const { shape, keys: shapeKeys } = this._getCached();
1047
- const dataKeys = util_1.util.objectKeys(ctx.data);
1048
- const extraKeys = dataKeys.filter((k) => !shapeKeys.includes(k));
1048
+ const extraKeys = [];
1049
+ for (const key in ctx.data) {
1050
+ if (!shapeKeys.includes(key)) {
1051
+ extraKeys.push(key);
1052
+ }
1053
+ }
1049
1054
  const pairs = [];
1050
1055
  for (const key of shapeKeys) {
1051
1056
  const keyValidator = shape[key];
1052
1057
  const value = ctx.data[key];
1053
1058
  pairs.push({
1054
1059
  key: { status: "valid", value: key },
1055
- value: keyValidator._parse({
1056
- parent: ctx,
1057
- data: value,
1058
- path: [...ctx.path, key],
1059
- }),
1060
+ value: keyValidator._parse(new ParseInputLazyPath(ctx, value, ctx.path, key)),
1060
1061
  alwaysSet: key in ctx.data,
1061
1062
  });
1062
1063
  }
@@ -1092,7 +1093,7 @@ class ZodObject extends ZodType {
1092
1093
  const value = ctx.data[key];
1093
1094
  pairs.push({
1094
1095
  key: { status: "valid", value: key },
1095
- value: catchall._parse({ parent: ctx, path: [...ctx.path, key], data: value } //, ctx.child(key), value, getParsedType(value)
1096
+ value: catchall._parse(new ParseInputLazyPath(ctx, value, ctx.path, key) //, ctx.child(key), value, getParsedType(value)
1096
1097
  ),
1097
1098
  alwaysSet: key in ctx.data,
1098
1099
  });
@@ -1592,11 +1593,7 @@ class ZodTuple extends ZodType {
1592
1593
  const schema = this._def.items[itemIndex] || this._def.rest;
1593
1594
  if (!schema)
1594
1595
  return null;
1595
- return schema._parse({
1596
- data: item,
1597
- path: [...ctx.path, itemIndex],
1598
- parent: ctx,
1599
- });
1596
+ return schema._parse(new ParseInputLazyPath(ctx, item, ctx.path, itemIndex));
1600
1597
  })
1601
1598
  .filter((x) => !!x); // filter nulls
1602
1599
  if (ctx.common.async) {
@@ -1649,16 +1646,8 @@ class ZodRecord extends ZodType {
1649
1646
  const valueType = this._def.valueType;
1650
1647
  for (const key in ctx.data) {
1651
1648
  pairs.push({
1652
- key: keyType._parse({
1653
- data: key,
1654
- path: [...ctx.path, key],
1655
- parent: ctx,
1656
- }),
1657
- value: valueType._parse({
1658
- data: ctx.data[key],
1659
- path: [...ctx.path, key],
1660
- parent: ctx,
1661
- }),
1649
+ key: keyType._parse(new ParseInputLazyPath(ctx, key, ctx.path, key)),
1650
+ value: valueType._parse(new ParseInputLazyPath(ctx, ctx.data[key], ctx.path, key)),
1662
1651
  });
1663
1652
  }
1664
1653
  if (ctx.common.async) {
@@ -1704,16 +1693,8 @@ class ZodMap extends ZodType {
1704
1693
  const valueType = this._def.valueType;
1705
1694
  const pairs = [...ctx.data.entries()].map(([key, value], index) => {
1706
1695
  return {
1707
- key: keyType._parse({
1708
- data: key,
1709
- path: [...ctx.path, index, "key"],
1710
- parent: ctx,
1711
- }),
1712
- value: valueType._parse({
1713
- data: value,
1714
- path: [...ctx.path, index, "value"],
1715
- parent: ctx,
1716
- }),
1696
+ key: keyType._parse(new ParseInputLazyPath(ctx, key, ctx.path, [index, "key"])),
1697
+ value: valueType._parse(new ParseInputLazyPath(ctx, value, ctx.path, [index, "value"])),
1717
1698
  };
1718
1699
  });
1719
1700
  if (ctx.common.async) {
@@ -1807,7 +1788,7 @@ class ZodSet extends ZodType {
1807
1788
  }
1808
1789
  return { status: status.value, value: parsedSet };
1809
1790
  }
1810
- const elements = [...ctx.data.values()].map((item, i) => valueType._parse({ data: item, path: [...ctx.path, i], parent: ctx }));
1791
+ const elements = [...ctx.data.values()].map((item, i) => valueType._parse(new ParseInputLazyPath(ctx, item, ctx.path, i)));
1811
1792
  if (ctx.common.async) {
1812
1793
  return Promise.all(elements).then((elements) => finalizeSet(elements));
1813
1794
  }
@@ -1988,9 +1969,8 @@ class ZodLiteral extends ZodType {
1988
1969
  if (input.data !== this._def.value) {
1989
1970
  const ctx = this._getOrReturnCtx(input);
1990
1971
  (0, parseUtil_1.addIssueToContext)(ctx, {
1991
- code: ZodError_1.ZodIssueCode.invalid_type,
1992
- expected: (0, parseUtil_1.getParsedType)(this._def.value),
1993
- received: ctx.parsedType,
1972
+ code: ZodError_1.ZodIssueCode.invalid_literal,
1973
+ expected: this._def.value,
1994
1974
  });
1995
1975
  return parseUtil_1.INVALID;
1996
1976
  }
package/package.json CHANGED
@@ -1,21 +1,22 @@
1
1
  {
2
2
  "name": "zod",
3
- "version": "3.14.2",
3
+ "version": "3.14.5",
4
4
  "description": "TypeScript-first schema declaration and validation library with static type inference",
5
5
  "main": "./lib/index.js",
6
- "types": "./lib/index.d.ts",
6
+ "types": "./index.d.ts",
7
7
  "module": "./lib/index.mjs",
8
8
  "dependencies": {},
9
9
  "exports": {
10
10
  ".": {
11
11
  "require": "./lib/index.js",
12
12
  "import": "./lib/index.mjs",
13
- "types": "./lib/index.d.ts"
13
+ "types": "./index.d.ts"
14
14
  },
15
15
  "./package.json": "./package.json"
16
16
  },
17
17
  "files": [
18
- "/lib"
18
+ "/lib",
19
+ "/index.d.ts"
19
20
  ],
20
21
  "repository": {
21
22
  "type": "git",
@@ -43,7 +44,7 @@
43
44
  ],
44
45
  "scripts": {
45
46
  "prettier:check": "prettier --check src/**/*.ts deno/lib/**/*.ts --no-error-on-unmatched-pattern",
46
- "prettier:fix": "prettier --write src/**/*.ts deno/lib/**/*.ts --no-error-on-unmatched-pattern",
47
+ "prettier:fix": "prettier --write src/**/*.ts deno/lib/**/*.ts --ignore-unknown --no-error-on-unmatched-pattern",
47
48
  "lint:check": "eslint --ext .ts ./src",
48
49
  "lint:fix": "eslint --fix --ext .ts ./src",
49
50
  "check": "yarn lint:check && yarn prettier:check",
@@ -57,51 +58,44 @@
57
58
  "rollup": "rollup --config rollup.config.js",
58
59
  "test": "jest --coverage",
59
60
  "test:deno": "cd deno && deno test",
60
- "badge": "make-coverage-badge --output-path ./coverage.svg",
61
61
  "prepublishOnly": "npm run test && npm run build && npm run build:deno",
62
62
  "play": "nodemon -e ts -w . -x ts-node src/playground.ts --project tsconfig.json --trace-warnings",
63
63
  "depcruise": "depcruise -c .dependency-cruiser.js src",
64
- "benchmark": "ts-node src/benchmarks/index.ts"
64
+ "benchmark": "ts-node src/benchmarks/index.ts",
65
+ "prepare": "husky install"
65
66
  },
66
67
  "devDependencies": {
67
68
  "@rollup/plugin-typescript": "^8.2.0",
68
69
  "@types/benchmark": "^2.1.0",
69
70
  "@types/jest": "^26.0.17",
70
- "@types/node": "^14.14.10",
71
+ "@types/node": "14",
71
72
  "@typescript-eslint/eslint-plugin": "^5.15.0",
72
73
  "@typescript-eslint/parser": "^5.15.0",
73
74
  "benchmark": "^2.1.4",
74
75
  "dependency-cruiser": "^9.19.0",
75
- "eslint": "^7.15.0",
76
- "eslint-config-prettier": "^7.1.0",
77
- "eslint-plugin-ban": "^1.5.2",
78
- "eslint-plugin-import": "^2.22.1",
76
+ "eslint": "^8.11.0",
77
+ "eslint-config-prettier": "^8.5.0",
78
+ "eslint-plugin-ban": "^1.6.0",
79
+ "eslint-plugin-import": "^2.25.4",
79
80
  "eslint-plugin-simple-import-sort": "^7.0.0",
80
- "eslint-plugin-unused-imports": "^1.1.0",
81
- "husky": "^4.3.4",
82
- "jest": "^26.6.3",
83
- "lint-staged": "^10.5.3",
84
- "make-coverage-badge": "^1.2.0",
85
- "nodemon": "^2.0.2",
86
- "prettier": "^2.2.1",
87
- "rollup": "^2.42.1",
88
- "rollup-plugin-uglify": "^6.0.4",
89
- "table": "^6.8.0",
90
- "ts-jest": "^26.4.4",
91
- "ts-node": "^9.1.0",
81
+ "eslint-plugin-unused-imports": "^2.0.0",
82
+ "husky": "^7.0.4",
83
+ "jest": "^27.5.1",
84
+ "lint-staged": "^12.3.7",
85
+ "nodemon": "^2.0.15",
86
+ "prettier": "^2.6.0",
87
+ "pretty-quick": "^3.1.3",
88
+ "rollup": "^2.70.1",
89
+ "ts-jest": "^27.1.3",
90
+ "ts-morph": "^14.0.0",
91
+ "ts-node": "^10.7.0",
92
92
  "tslib": "^2.3.1",
93
93
  "typescript": "^4.6.2"
94
94
  },
95
- "husky": {
96
- "hooks": {
97
- "pre-commit": "lint-staged",
98
- "pre-push": "lint-staged"
99
- }
100
- },
101
95
  "lint-staged": {
102
- "*.ts": [
103
- "yarn lint:fix",
104
- "yarn prettier:fix"
96
+ "src/*.ts": [
97
+ "eslint --cache --fix",
98
+ "prettier --ignore-unknown --write"
105
99
  ]
106
100
  }
107
101
  }