@exodus/zod 3.24.2 → 3.24.4

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/index.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export * from "./lib";
1
+ export * from "./lib/index.js";
2
2
  export as namespace Zod;
package/lib/ZodError.d.ts CHANGED
@@ -1,6 +1,6 @@
1
- import type { TypeOf, ZodType } from ".";
2
- import { Primitive } from "./helpers/typeAliases";
3
- import { util, ZodParsedType } from "./helpers/util";
1
+ import { Primitive } from "./helpers/typeAliases.js";
2
+ import { util, ZodParsedType } from "./helpers/util.js";
3
+ import type { TypeOf, ZodType } from "./index.js";
4
4
  type allKeys<T> = T extends any ? keyof T : never;
5
5
  export type inferFlattenedErrors<T extends ZodType<any, any, any>, U = string> = typeToFlattenedError<TypeOf<T>, U>;
6
6
  export type typeToFlattenedError<T, U = string> = {
package/lib/errors.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import defaultErrorMap from "./locales/en";
2
- import type { ZodErrorMap } from "./ZodError";
1
+ import defaultErrorMap from "./locales/en.js";
2
+ import type { ZodErrorMap } from "./ZodError.js";
3
3
  export { defaultErrorMap };
4
4
  export declare function setErrorMap(map: ZodErrorMap): void;
5
5
  export declare function getErrorMap(): ZodErrorMap;
package/lib/external.d.ts CHANGED
@@ -1,6 +1,6 @@
1
- export * from "./errors";
2
- export * from "./helpers/parseUtil";
3
- export * from "./helpers/typeAliases";
4
- export * from "./helpers/util";
5
- export * from "./types";
6
- export * from "./ZodError";
1
+ export * from "./errors.js";
2
+ export * from "./helpers/parseUtil.js";
3
+ export * from "./helpers/typeAliases.js";
4
+ export * from "./helpers/util.js";
5
+ export * from "./types.js";
6
+ export * from "./ZodError.js";
@@ -1,5 +1,5 @@
1
- import type { IssueData, ZodErrorMap, ZodIssue } from "../ZodError";
2
- import type { ZodParsedType } from "./util";
1
+ import type { IssueData, ZodErrorMap, ZodIssue } from "../ZodError.js";
2
+ import type { ZodParsedType } from "./util.js";
3
3
  export declare const makeIssue: (params: {
4
4
  data: any;
5
5
  path: (string | number)[];
@@ -1,4 +1,4 @@
1
- import type { ZodArray, ZodNullable, ZodObject, ZodOptional, ZodRawShape, ZodTuple, ZodTupleItems, ZodTypeAny } from "../index";
1
+ import type { ZodArray, ZodNullable, ZodObject, ZodOptional, ZodRawShape, ZodTuple, ZodTupleItems, ZodTypeAny } from "../index.js";
2
2
  export declare namespace partialUtil {
3
3
  type DeepPartial<T extends ZodTypeAny> = T extends ZodObject<ZodRawShape> ? ZodObject<{
4
4
  [k in keyof T["shape"]]: ZodOptional<DeepPartial<T["shape"][k]>>;
package/lib/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import * as z from "./external";
2
- export * from "./external";
1
+ import * as z from "./external.js";
2
+ export * from "./external.js";
3
3
  export { z };
4
4
  export default z;
package/lib/index.mjs CHANGED
@@ -166,36 +166,42 @@ class ZodError extends Error {
166
166
  function (issue) {
167
167
  return issue.message;
168
168
  };
169
- const fieldErrors = { _errors: [] };
170
- const processError = (error) => {
171
- for (const issue of error.issues) {
172
- if (issue.code === "invalid_union") {
173
- issue.unionErrors.map(processError);
174
- }
175
- else if (issue.code === "invalid_return_type") {
176
- processError(issue.returnTypeError);
177
- }
178
- else if (issue.code === "invalid_arguments") {
179
- processError(issue.argumentsError);
180
- }
181
- else if (issue.path.length === 0) {
182
- fieldErrors._errors.push(mapper(issue));
183
- }
184
- else {
185
- let curr = fieldErrors;
186
- let i = 0;
187
- while (i < issue.path.length) {
188
- const el = issue.path[i];
189
- const terminal = i === issue.path.length - 1;
190
- if (!terminal) {
191
- curr[el] = curr[el] || { _errors: [] };
169
+ const fieldErrors = { __proto__: null, _errors: [] };
170
+ const processError = (rootError) => {
171
+ const stack = [rootError];
172
+ while (stack.length > 0) {
173
+ const error = stack.pop();
174
+ for (const issue of error.issues) {
175
+ if (issue.code === "invalid_union") {
176
+ for (const unionError of issue.unionErrors) {
177
+ stack.push(unionError);
192
178
  }
193
- else {
194
- curr[el] = curr[el] || { _errors: [] };
195
- curr[el]._errors.push(mapper(issue));
179
+ }
180
+ else if (issue.code === "invalid_return_type") {
181
+ stack.push(issue.returnTypeError);
182
+ }
183
+ else if (issue.code === "invalid_arguments") {
184
+ stack.push(issue.argumentsError);
185
+ }
186
+ else if (issue.path.length === 0) {
187
+ fieldErrors._errors.push(mapper(issue));
188
+ }
189
+ else {
190
+ let curr = fieldErrors;
191
+ let i = 0;
192
+ while (i < issue.path.length) {
193
+ const el = issue.path[i];
194
+ const terminal = i === issue.path.length - 1;
195
+ if (!terminal) {
196
+ curr[el] = curr[el] || { _errors: [] };
197
+ }
198
+ else {
199
+ curr[el] = curr[el] || { _errors: [] };
200
+ curr[el]._errors.push(mapper(issue));
201
+ }
202
+ curr = curr[el];
203
+ i++;
196
204
  }
197
- curr = curr[el];
198
- i++;
199
205
  }
200
206
  }
201
207
  }
@@ -2677,48 +2683,53 @@ class ZodUnion extends ZodType {
2677
2683
  };
2678
2684
  }
2679
2685
  const getDiscriminator = (type) => {
2680
- if (type instanceof ZodLazy) {
2681
- return getDiscriminator(type.schema);
2682
- }
2683
- else if (type instanceof ZodEffects) {
2684
- return getDiscriminator(type.innerType());
2685
- }
2686
- else if (type instanceof ZodLiteral) {
2687
- return [type.value];
2688
- }
2689
- else if (type instanceof ZodEnum) {
2690
- return type.options;
2691
- }
2692
- else if (type instanceof ZodNativeEnum) {
2693
- return util.objectValues(type.enum);
2694
- }
2695
- else if (type instanceof ZodDefault) {
2696
- return getDiscriminator(type._def.innerType);
2697
- }
2698
- else if (type instanceof ZodUndefined) {
2699
- return [undefined];
2700
- }
2701
- else if (type instanceof ZodNull) {
2702
- return [null];
2703
- }
2704
- else if (type instanceof ZodOptional) {
2705
- return [undefined, ...getDiscriminator(type.unwrap())];
2706
- }
2707
- else if (type instanceof ZodNullable) {
2708
- return [null, ...getDiscriminator(type.unwrap())];
2709
- }
2710
- else if (type instanceof ZodBranded) {
2711
- return getDiscriminator(type.unwrap());
2712
- }
2713
- else if (type instanceof ZodReadonly) {
2714
- return getDiscriminator(type.unwrap());
2715
- }
2716
- else if (type instanceof ZodCatch) {
2717
- return getDiscriminator(type._def.innerType);
2718
- }
2719
- else {
2720
- return [];
2686
+ const result = [];
2687
+ const stack = [type];
2688
+ while (stack.length > 0) {
2689
+ const current = stack.pop();
2690
+ if (current instanceof ZodLazy) {
2691
+ stack.push(current.schema);
2692
+ }
2693
+ else if (current instanceof ZodEffects) {
2694
+ stack.push(current.innerType());
2695
+ }
2696
+ else if (current instanceof ZodLiteral) {
2697
+ result.push(current.value);
2698
+ }
2699
+ else if (current instanceof ZodEnum) {
2700
+ result.push(...current.options);
2701
+ }
2702
+ else if (current instanceof ZodNativeEnum) {
2703
+ result.push(...util.objectValues(current.enum));
2704
+ }
2705
+ else if (current instanceof ZodDefault) {
2706
+ stack.push(current._def.innerType);
2707
+ }
2708
+ else if (current instanceof ZodUndefined) {
2709
+ result.push(undefined);
2710
+ }
2711
+ else if (current instanceof ZodNull) {
2712
+ result.push(null);
2713
+ }
2714
+ else if (current instanceof ZodOptional) {
2715
+ result.push(undefined);
2716
+ stack.push(current.unwrap());
2717
+ }
2718
+ else if (current instanceof ZodNullable) {
2719
+ result.push(null);
2720
+ stack.push(current.unwrap());
2721
+ }
2722
+ else if (current instanceof ZodBranded) {
2723
+ stack.push(current.unwrap());
2724
+ }
2725
+ else if (current instanceof ZodReadonly) {
2726
+ stack.push(current.unwrap());
2727
+ }
2728
+ else if (current instanceof ZodCatch) {
2729
+ stack.push(current._def.innerType);
2730
+ }
2721
2731
  }
2732
+ return result;
2722
2733
  };
2723
2734
  class ZodDiscriminatedUnion extends ZodType {
2724
2735
  _parse(input) {
@@ -2790,50 +2801,66 @@ class ZodDiscriminatedUnion extends ZodType {
2790
2801
  }
2791
2802
  }
2792
2803
  function mergeValues(a, b) {
2793
- const aType = getParsedType(a);
2794
- const bType = getParsedType(b);
2795
- if (a === b) {
2796
- return { valid: true, data: a };
2797
- }
2798
- else if (aType === ZodParsedType.object && bType === ZodParsedType.object) {
2799
- const bKeys = util.objectKeys(b);
2800
- const sharedKeys = util
2801
- .objectKeys(a)
2802
- .filter((key) => bKeys.indexOf(key) !== -1);
2803
- const newObj = { __proto__: null, ...a, ...b };
2804
- for (const key of sharedKeys) {
2805
- const sharedValue = mergeValues(a[key], b[key]);
2806
- if (!sharedValue.valid) {
2807
- return { valid: false };
2808
- }
2809
- newObj[key] = sharedValue.data;
2810
- }
2811
- return { valid: true, data: newObj };
2812
- }
2813
- else if (aType === ZodParsedType.array && bType === ZodParsedType.array) {
2814
- if (a.length !== b.length) {
2815
- return { valid: false };
2816
- }
2817
- const newArray = [];
2818
- for (let index = 0; index < a.length; index++) {
2819
- const itemA = a[index];
2820
- const itemB = b[index];
2821
- const sharedValue = mergeValues(itemA, itemB);
2822
- if (!sharedValue.valid) {
2823
- return { valid: false };
2824
- }
2825
- newArray.push(sharedValue.data);
2826
- }
2827
- return { valid: true, data: newArray };
2828
- }
2829
- else if (aType === ZodParsedType.date &&
2830
- bType === ZodParsedType.date &&
2831
- +a === +b) {
2832
- return { valid: true, data: a };
2804
+ const root = { _data: undefined };
2805
+ let valid = true;
2806
+ const stack = [
2807
+ { a, b, parent: root, key: "_data" },
2808
+ ];
2809
+ while (stack.length > 0 && valid) {
2810
+ const { a, b, parent, key } = stack.pop();
2811
+ const aType = getParsedType(a);
2812
+ const bType = getParsedType(b);
2813
+ if (a === b) {
2814
+ parent[key] = a;
2815
+ continue;
2816
+ }
2817
+ if (aType === ZodParsedType.object && bType === ZodParsedType.object) {
2818
+ const bKeys = util.objectKeys(b);
2819
+ const sharedKeys = util
2820
+ .objectKeys(a)
2821
+ .filter((k) => bKeys.indexOf(k) !== -1);
2822
+ const newObj = { __proto__: null, ...a, ...b };
2823
+ parent[key] = newObj;
2824
+ for (const sharedKey of sharedKeys) {
2825
+ stack.push({
2826
+ a: a[sharedKey],
2827
+ b: b[sharedKey],
2828
+ parent: newObj,
2829
+ key: sharedKey,
2830
+ });
2831
+ }
2832
+ continue;
2833
+ }
2834
+ if (aType === ZodParsedType.array && bType === ZodParsedType.array) {
2835
+ if (a.length !== b.length) {
2836
+ valid = false;
2837
+ break;
2838
+ }
2839
+ const newArray = [];
2840
+ parent[key] = newArray;
2841
+ for (let i = 0; i < a.length; i++) {
2842
+ newArray[i] = undefined;
2843
+ stack.push({
2844
+ a: a[i],
2845
+ b: b[i],
2846
+ parent: newArray,
2847
+ key: i,
2848
+ });
2849
+ }
2850
+ continue;
2851
+ }
2852
+ if (aType === ZodParsedType.date &&
2853
+ bType === ZodParsedType.date &&
2854
+ +a === +b) {
2855
+ parent[key] = a;
2856
+ continue;
2857
+ }
2858
+ valid = false;
2833
2859
  }
2834
- else {
2860
+ if (!valid) {
2835
2861
  return { valid: false };
2836
2862
  }
2863
+ return { valid: true, data: root._data };
2837
2864
  }
2838
2865
  class ZodIntersection extends ZodType {
2839
2866
  _parse(input) {
@@ -1,3 +1,3 @@
1
- import { ZodErrorMap } from "../ZodError";
1
+ import { ZodErrorMap } from "../ZodError.js";
2
2
  declare const errorMap: ZodErrorMap;
3
3
  export default errorMap;
package/lib/types.d.ts CHANGED
@@ -1,11 +1,11 @@
1
- import { enumUtil } from "./helpers/enumUtil";
2
- import { errorUtil } from "./helpers/errorUtil";
3
- import { AsyncParseReturnType, INVALID, ParseContext, ParseInput, ParseParams, ParseReturnType, ParseStatus, SyncParseReturnType } from "./helpers/parseUtil";
4
- import { partialUtil } from "./helpers/partialUtil";
5
- import { Primitive } from "./helpers/typeAliases";
6
- import { objectUtil, util } from "./helpers/util";
7
- import type { StandardSchemaV1 } from "./standard-schema";
8
- import { IssueData, StringValidation, ZodCustomIssue, ZodError, ZodErrorMap } from "./ZodError";
1
+ import { enumUtil } from "./helpers/enumUtil.js";
2
+ import { errorUtil } from "./helpers/errorUtil.js";
3
+ import { AsyncParseReturnType, INVALID, ParseContext, ParseInput, ParseParams, ParseReturnType, ParseStatus, SyncParseReturnType } from "./helpers/parseUtil.js";
4
+ import { partialUtil } from "./helpers/partialUtil.js";
5
+ import { Primitive } from "./helpers/typeAliases.js";
6
+ import { objectUtil, util } from "./helpers/util.js";
7
+ import type { StandardSchemaV1 } from "./standard-schema.js";
8
+ import { IssueData, StringValidation, ZodCustomIssue, ZodError, ZodErrorMap } from "./ZodError.js";
9
9
  export interface RefinementCtx {
10
10
  addIssue: (arg: IssueData) => void;
11
11
  path: (string | number)[];
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "@exodus/zod",
3
- "version": "3.24.2",
3
+ "version": "3.24.4",
4
4
  "author": "Exodus Movement, Inc.",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+https://github.com/ExodusMovement/zod.git"
8
8
  },
9
9
  "type": "module",
10
+ "main": "./lib/index.mjs",
10
11
  "module": "./lib/index.mjs",
11
12
  "devDependencies": {
12
13
  "@babel/core": "^7.22.5",