zod-compare 0.1.4 → 0.3.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 CHANGED
@@ -1,4 +1,4 @@
1
- # Zod Compare
1
+ # ⚖️ Zod Compare
2
2
 
3
3
  [![Build](https://github.com/lawvs/zod-compare/actions/workflows/build.yml/badge.svg)](https://github.com/lawvs/zod-compare/actions/workflows/build.yml)
4
4
  [![npm](https://img.shields.io/npm/v/zod-compare)](https://www.npmjs.com/package/zod-compare)
@@ -10,7 +10,14 @@ Compare two [Zod](https://zod.dev/) schemas recursively.
10
10
  ## Installation
11
11
 
12
12
  ```bash
13
- npm install zod zod-compare --save
13
+ # npm
14
+ npm install zod zod-compare
15
+
16
+ # yarn
17
+ yarn add zod zod-compare
18
+
19
+ # pnpm
20
+ pnpm add zod zod-compare
14
21
  ```
15
22
 
16
23
  ## Usage
@@ -36,35 +43,57 @@ isCompatibleType(
36
43
 
37
44
  ## Advanced Usage
38
45
 
39
- `isSameType` also offers an `interceptor` option, enabling you to tailor the comparison process according to your needs.
46
+ ### Custom Rules
47
+
48
+ You can use `createCompareFn` to create a custom comparison function.
40
49
 
41
50
  ```ts
42
- isSameType(
43
- z.object({ name: z.string(), other: z.number() }),
44
- z.object({ name: z.string() }),
45
- {
46
- interceptor: (a, b) => {
47
- if (a instanceof z.ZodObject && b instanceof z.ZodObject) {
48
- // return boolean to override the comparison result
49
- return a.shape.name === b.shape.name;
50
- }
51
- // return other values to use the default comparison
52
- },
51
+ import {
52
+ createCompareFn,
53
+ isSameTypePresetRules,
54
+ defineCompareRule,
55
+ } from "zod-compare";
56
+
57
+ const customRule = defineCompareRule(
58
+ "compare description",
59
+ (a, b, next, recheck, context) => {
60
+ // If the schemas are not having the same description, return false
61
+ if (a.description !== b.description) {
62
+ return false;
63
+ }
64
+ return next();
53
65
  },
54
66
  );
55
- // true
67
+
68
+ const strictIsSameType = createCompareFn([
69
+ customRule,
70
+ ...isSameTypePresetRules,
71
+ ]);
56
72
  ```
57
73
 
58
- ## Caveats
74
+ ## Debugging
59
75
 
60
- `zod-compare` is designed to compare Zod schemas created by the developer.
61
- We do not have plans to support comparing schemas that include custom validation functions or other advanced Zod features.
76
+ You can pass a `context` object to the comparison functions to get more information about the comparison process.
62
77
 
63
- This tool will disregard any custom validations like `min`, `max`, `length`, among others.
78
+ ```ts
79
+ const context = {
80
+ stacks: [],
81
+ };
82
+ isSameType(
83
+ z.object({ name: z.string(), other: z.number() }),
84
+ z.object({ name: z.string(), other: z.string() }),
85
+ context,
86
+ );
87
+
88
+ // type stacks = { name: string; target: [a: ZodType, b: ZodType]; }[]
89
+ console.log(context.stacks);
90
+ ```
91
+
92
+ ## Caveats
64
93
 
65
- Furthermore, it cannot be utilized for comparing `ZodLazy`, `ZodEffects`, `ZodDefault`, `ZodCatch`, `ZodPipeline`, `ZodTransformer`, `ZodError` types.
94
+ The default rules `isSameTypePresetRules` will disregard any custom validations like `min`, `max`, `length`, among others. Additionally, these default rules cannot be utilized for comparing `ZodLazy`, `ZodEffects`, `ZodDefault`, `ZodCatch`, `ZodPipeline`, `ZodTransformer`, `ZodError` types.
66
95
 
67
- But you can use the `interceptor` option to customize the comparison process.
96
+ If there is a necessity to compare these types, custom rules can be established using `defineCompareRule`.
68
97
 
69
98
  ## API
70
99
 
@@ -73,25 +102,80 @@ But you can use the `interceptor` option to customize the comparison process.
73
102
  Compares two Zod schemas and returns `true` if they are the same.
74
103
 
75
104
  ```ts
76
- function isSameType(
77
- a: ZodType,
78
- b: ZodType,
79
- options?: {
80
- interceptor?: (a: ZodType, b: ZodType) => boolean | undefined;
81
- },
82
- ): boolean;
105
+ import { isSameType } from "zod-compare";
106
+
107
+ type isSameType: (a: ZodType, b: ZodType, context?: CompareContext) => boolean;
83
108
  ```
84
109
 
85
- ### `isCompatibleType` (Unstable API)
110
+ ### `createCompareFn`
111
+
112
+ Creates a custom comparison function.
86
113
 
87
114
  ```ts
88
- function isCompatibleType(
115
+ import { createCompareFn, defineCompareRule } from "zod-compare";
116
+
117
+ type defineCompareRule = (
118
+ name: string,
119
+ rule: CompareFn,
120
+ ) => {
121
+ name: string;
122
+ rule: CompareFn;
123
+ };
124
+
125
+ type createCompareFn = (rules: CompareRule[]) => typeof isSameType;
126
+
127
+ // Example
128
+ const isSameType = createCompareFn(isSameTypePresetRules);
129
+ const isCompatibleType = createCompareFn(isCompatibleTypePresetRules);
130
+ ```
131
+
132
+ ### `isCompatibleType` (Experimental API)
133
+
134
+ Compares two Zod schemas and returns `true` if they are compatible.
135
+
136
+ ```ts
137
+ import { isCompatibleType } from "zod-compare";
138
+ // The `higherType` should be a looser type
139
+ // The `lowerType` should be a stricter type
140
+ type isCompatibleType: (higherType: ZodType, lowerType: ZodType) => boolean;
141
+ ```
142
+
143
+ ### Preset Rules
144
+
145
+ You can use the preset rules `isSameTypePresetRules` and `isCompatibleTypePresetRules` to create custom comparison functions.
146
+
147
+ ```ts
148
+ import { isSameTypePresetRules, isCompatibleTypePresetRules } from "zod-compare";
149
+
150
+ type isSameTypePresetRules: CompareRule[];
151
+ type isCompatibleTypePresetRules: CompareRule[];
152
+
153
+ // Example
154
+ const yourIsSameType = createCompareFn([customRule, ...isSameTypePresetRules]);
155
+ ```
156
+
157
+ ### Types
158
+
159
+ ```ts
160
+ type CompareContext = {
161
+ stacks?: {
162
+ name: string;
163
+ target: [a: ZodType, b: ZodType];
164
+ }[];
165
+ } & Record<string, unknown>;
166
+
167
+ type CompareFn = (
89
168
  a: ZodType,
90
169
  b: ZodType,
91
- options?: {
92
- interceptor?: (a: ZodType, b: ZodType) => boolean | undefined;
93
- },
94
- ): boolean;
170
+ next: () => boolean,
171
+ recheck: (a: ZodType, b: ZodType) => boolean,
172
+ context: CompareContext,
173
+ ) => boolean;
174
+
175
+ type CompareRule = {
176
+ name: string;
177
+ compare: CompareFn;
178
+ };
95
179
  ```
96
180
 
97
181
  ## License
package/dist/index.cjs CHANGED
@@ -1,2 +1,2 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const t=require("zod"),m={deep:!0,ignoreOptional:!1,ignoreNullable:!1,ignoreReadOnly:!1,ignoreBranded:!1,ignoreValidations:!0,interceptor:()=>{}},Z=e=>e instanceof t.z.ZodString||e instanceof t.z.ZodNumber||e instanceof t.z.ZodNaN||e instanceof t.z.ZodBigInt||e instanceof t.z.ZodBoolean||e instanceof t.z.ZodDate||e instanceof t.z.ZodSymbol||e instanceof t.z.ZodUndefined||e instanceof t.z.ZodNull||e instanceof t.z.ZodAny||e instanceof t.z.ZodUnknown||e instanceof t.z.ZodNever||e instanceof t.z.ZodVoid,u=e=>e.options.flatMap(n=>n instanceof t.z.ZodUnion?u(n):n),s=(e,n,a)=>{const o={...m,...a},d=o.interceptor(e,n,o);if(d===!0||d===!1)return d;if(e===void 0||n===void 0)throw new Error("Failed to compare type! "+e+" "+n);if(e===n)return!0;if(e.constructor!==n.constructor)return!1;if(!("typeName"in e._def)||!("typeName"in n._def))throw new Error("Failed to compare type! "+e._def+" "+n._def);if(e._def.typeName!==n._def.typeName)return!1;if(o.ignoreBranded)return e instanceof t.z.ZodBranded&&(e=e.unwrap()),n instanceof t.z.ZodBranded&&(n=n.unwrap()),s(e,n,o);if(e instanceof t.z.ZodBranded||n instanceof t.z.ZodBranded)return!1;if("unwrap"in e&&typeof e.unwrap=="function")return"unwrap"in n&&typeof n.unwrap=="function"?s(e.unwrap(),n.unwrap(),o):!1;if(!o.ignoreOptional&&e.isOptional()!==n.isOptional()||!o.ignoreNullable&&e.isNullable()!==n.isNullable())return!1;if(Z(e))return!0;if(e instanceof t.z.ZodObject&&n instanceof t.z.ZodObject){const r=e.shape,i=n.shape;if(Object.keys(r).length!==Object.keys(i).length)return!1;for(const f in r)if(!(f in i)||!s(r[f],i[f],o))return!1;return!0}if(e instanceof t.z.ZodArray&&n instanceof t.z.ZodArray)return s(e.element,n.element,o);if(e instanceof t.z.ZodTuple&&n instanceof t.z.ZodTuple){if(e.items.length!==n.items.length)return!1;for(let r=0;r<e.items.length;r++)if(!s(e.items[r],n.items[r],o))return!1;return e._def.rest||n._def.rest?!e._def.rest||!n._def.rest?!1:s(e._def.rest,n._def.rest,o):!0}if(e instanceof t.z.ZodLiteral&&n instanceof t.z.ZodLiteral)return e.value===n.value;if(e instanceof t.z.ZodIntersection&&n instanceof t.z.ZodIntersection)return s(e._def.left,n._def.left,o)&&s(e._def.right,n._def.right,o)||s(e._def.left,n._def.right,o)&&s(e._def.right,n._def.left,o);if(e instanceof t.z.ZodUnion&&n instanceof t.z.ZodUnion){const r=u(e),i=u(n);if(r.length!==i.length)return!1;for(let f of r){let l=i.findIndex(z=>s(f,z,o));if(l===-1)return!1;i.splice(l,1)}return i.length===0}if(e instanceof t.z.ZodReadonly&&n instanceof t.z.ZodReadonly)return s(e._def.innerType,n._def.innerType,o);if(e instanceof t.z.ZodRecord&&n instanceof t.z.ZodRecord||e instanceof t.z.ZodMap&&n instanceof t.z.ZodMap)return s(e.keySchema,n.keySchema,o)&&s(e.valueSchema,n.valueSchema,o);if(e instanceof t.z.ZodSet&&n instanceof t.z.ZodSet)return s(e._def.valueType,n._def.valueType,o);if(e instanceof t.z.ZodFunction&&n instanceof t.z.ZodFunction)return s(e.parameters(),n.parameters(),o)&&s(e.returnType(),n.returnType(),o);if(e instanceof t.z.ZodEnum&&n instanceof t.z.ZodEnum){const r=e.options,i=n.options;if(r.length!==i.length)return!1;for(let f=0;f<r.length;f++)if(r[f]!==i[f])return!1;return!0}if(e instanceof t.z.ZodNativeEnum&&n instanceof t.z.ZodNativeEnum){const r=e.enum,i=n.enum;if(Object.keys(r).length!==Object.keys(i).length)return!1;for(const f in r)if(r[f]!==i[f])return!1;return!0}throw console.error('Failed to compare type! "'+e,n),new Error("Unknown type! "+e._def.typeName+" "+n._def.typeName)},c=(e,n,a)=>{const o={...m,...a},d=o.interceptor(e,n,o);if(d===!0||d===!1)return d;if(s(e,n,o))return!0;if(!("typeName"in e._def)||!("typeName"in n._def))throw new Error("Failed to compare type! "+e._def+" "+n._def);if(n instanceof t.z.ZodOptional||n instanceof t.z.ZodNullable)return c(e,n.unwrap(),o);if(e instanceof t.z.ZodUnion&&n instanceof t.z.ZodUnion){const r=u(e),i=u(n);for(let f=0;f<r.length;f++)if(!i.some(z=>c(r[f],z,o)))return!1;return!0}if(e instanceof t.z.ZodUnion)return u(e).every(i=>c(i,n,o));if(n instanceof t.z.ZodUnion)return u(n).some(i=>c(e,i,o));if(e.constructor!==n.constructor)return!1;if(e instanceof t.z.ZodObject&&n instanceof t.z.ZodObject){const r=e.shape,i=n.shape;if(Object.keys(r).length<Object.keys(i).length)return!1;for(const f in i)if(!(f in r)||!c(r[f],i[f],o))return!1;return!0}if(e instanceof t.z.ZodArray&&n instanceof t.z.ZodArray)return c(e.element,n.element,o);if(e instanceof t.z.ZodTuple&&n instanceof t.z.ZodTuple){if(e.items.length<n.items.length)return!1;for(let r=0;r<n.items.length;r++)if(!c(e.items[r],n.items[r],o))return!1;return n._def.rest?e._def.rest?c(e._def.rest,n._def.rest,o):!1:!0}throw new Error("Failed to compare types!"+e._def.typeName+" "+n._def.typeName)};exports.isCompatibleType=c;exports.isSameType=s;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const r=require("zod"),m=e=>{const n=(t,o,a={})=>{let i=-1;const s=f=>{if(f===e.length)throw new Error("Failed to compare type! "+t+" "+o);if(f===i)throw new Error("next() called multiple times");i=f;const u=e[f];return"stacks"in a&&Array.isArray(a.stacks)&&a.stacks.push({name:u.name,target:[t,o]}),u.compare(t,o,()=>s(f+1),(z,y)=>n(z,y,a),a)};return s(0)};return n},_=m,d=e=>e instanceof r.z.ZodString||e instanceof r.z.ZodNumber||e instanceof r.z.ZodNaN||e instanceof r.z.ZodBigInt||e instanceof r.z.ZodBoolean||e instanceof r.z.ZodDate||e instanceof r.z.ZodSymbol||e instanceof r.z.ZodUndefined||e instanceof r.z.ZodNull||e instanceof r.z.ZodAny||e instanceof r.z.ZodUnknown||e instanceof r.z.ZodNever||e instanceof r.z.ZodVoid,c=e=>e.options.flatMap(n=>n instanceof r.z.ZodUnion?c(n):n),p=[{name:"undefined check",compare:(e,n,t)=>{if(e===void 0||n===void 0)throw new Error("Failed to compare type! "+e+" "+n);return t()}},{name:"compare reference",compare:(e,n,t)=>e===n?!0:t()},{name:"compare constructor",compare:(e,n,t)=>e.constructor!==n.constructor?!1:t()},{name:"compare typeName",compare:(e,n,t)=>{if(!("typeName"in e._def)||!("typeName"in n._def))throw new Error("Failed to compare type! "+e._def+" "+n._def);return e._def.typeName!==n._def.typeName?!1:t()}},{name:"compare ZodBranded",compare:(e,n,t)=>e instanceof r.z.ZodBranded||n instanceof r.z.ZodBranded?!1:t()},{name:"unwrap ZodType",compare:(e,n,t,o)=>"unwrap"in e&&typeof e.unwrap=="function"?"unwrap"in n&&typeof n.unwrap=="function"?o(e.unwrap(),n.unwrap()):!1:t()},{name:"is same primitive",compare:(e,n,t)=>d(e)&&d(n)&&e.constructor===n.constructor?!0:t()},{name:"compare ZodObject",compare:(e,n,t,o)=>{if(e instanceof r.z.ZodObject&&n instanceof r.z.ZodObject){const a=e.shape,i=n.shape;if(Object.keys(a).length!==Object.keys(i).length)return!1;for(const s in a)if(!(s in i)||!o(a[s],i[s]))return!1;return!0}return t()}},{name:"compare ZodArray",compare:(e,n,t,o)=>e instanceof r.z.ZodArray&&n instanceof r.z.ZodArray?o(e.element,n.element):t()},{name:"compare ZodTuple",compare:(e,n,t,o)=>{if(e instanceof r.z.ZodTuple&&n instanceof r.z.ZodTuple){if(e.items.length!==n.items.length)return!1;for(let a=0;a<e.items.length;a++)if(!o(e.items[a],n.items[a]))return!1;return e._def.rest||n._def.rest?!e._def.rest||!n._def.rest?!1:o(e._def.rest,n._def.rest):!0}return t()}},{name:"compare ZodLiteral",compare:(e,n,t)=>e instanceof r.z.ZodLiteral&&n instanceof r.z.ZodLiteral?e.value===n.value:t()},{name:"compare ZodIntersection",compare:(e,n,t,o)=>e instanceof r.z.ZodIntersection&&n instanceof r.z.ZodIntersection?o(e._def.left,n._def.left)&&o(e._def.right,n._def.right)||o(e._def.left,n._def.right)&&o(e._def.right,n._def.left):t()},{name:"compare ZodUnion",compare:(e,n,t,o)=>{if(e instanceof r.z.ZodUnion&&n instanceof r.z.ZodUnion){const a=c(e),i=c(n);if(a.length!==i.length)return!1;for(let s of a){let f=i.findIndex(u=>o(s,u));if(f===-1)return!1;i.splice(f,1)}return i.length===0}return t()}},{name:"compare ZodReadonly",compare:(e,n,t,o)=>e instanceof r.z.ZodReadonly&&n instanceof r.z.ZodReadonly?o(e._def.innerType,n._def.innerType):t()},{name:"compare ZodRecord",compare:(e,n,t,o)=>e instanceof r.z.ZodRecord&&n instanceof r.z.ZodRecord?o(e.keySchema,n.keySchema)&&o(e.valueSchema,n.valueSchema):t()},{name:"compare ZodMap",compare:(e,n,t,o)=>e instanceof r.z.ZodMap&&n instanceof r.z.ZodMap?o(e.keySchema,n.keySchema)&&o(e.valueSchema,n.valueSchema):t()},{name:"compare ZodSet",compare:(e,n,t,o)=>e instanceof r.z.ZodSet&&n instanceof r.z.ZodSet?o(e._def.valueType,n._def.valueType):t()},{name:"compare ZodFunction",compare:(e,n,t,o)=>e instanceof r.z.ZodFunction&&n instanceof r.z.ZodFunction?o(e.parameters(),n.parameters())&&o(e.returnType(),n.returnType()):t()},{name:"compare ZodEnum",compare:(e,n,t)=>{if(e instanceof r.z.ZodEnum&&n instanceof r.z.ZodEnum){const o=e.options,a=n.options;if(o.length!==a.length)return!1;for(let i=0;i<o.length;i++)if(o[i]!==a[i])return!1;return!0}return t()}},{name:"compare ZodNativeEnum",compare:(e,n,t)=>{if(e instanceof r.z.ZodNativeEnum&&n instanceof r.z.ZodNativeEnum){const o=e.enum,a=n.enum;if(Object.keys(o).length!==Object.keys(a).length)return!1;for(const i in o)if(o[i]!==a[i])return!1;return!0}return t()}}],l=m(p),Z=[{name:"is same type",compare:(e,n,t)=>l(e,n)?!0:t()},{name:"check typeName",compare:(e,n,t)=>{if(!("typeName"in e._def)||!("typeName"in n._def))throw new Error("Failed to compare type! "+e._def+" "+n._def);return t()}},{name:"check ZodOptional/ZodNullable",compare:(e,n,t,o)=>n instanceof r.z.ZodOptional||n instanceof r.z.ZodNullable?o(e,n.unwrap()):t()},{name:"check ZodUnion",compare:(e,n,t,o)=>{if(e instanceof r.z.ZodUnion&&n instanceof r.z.ZodUnion){const a=c(e),i=c(n);for(let s=0;s<a.length;s++)if(!i.some(u=>o(a[s],u)))return!1;return!0}return e instanceof r.z.ZodUnion?c(e).every(i=>o(i,n)):n instanceof r.z.ZodUnion?c(n).some(i=>o(e,i)):t()}},{name:"compare constructor",compare:(e,n,t)=>e.constructor!==n.constructor?!1:t()},{name:"check ZodObject",compare:(e,n,t,o)=>{if(e instanceof r.z.ZodObject&&n instanceof r.z.ZodObject){const a=e.shape,i=n.shape;if(Object.keys(a).length<Object.keys(i).length)return!1;for(const s in i)if(!(s in a)||!o(a[s],i[s]))return!1;return!0}return t()}},{name:"check ZodArray",compare:(e,n,t,o)=>e instanceof r.z.ZodArray&&n instanceof r.z.ZodArray?o(e.element,n.element):t()},{name:"check ZodTuple",compare:(e,n,t,o)=>{if(e instanceof r.z.ZodTuple&&n instanceof r.z.ZodTuple){if(e.items.length<n.items.length)return!1;for(let a=0;a<n.items.length;a++)if(!o(e.items[a],n.items[a]))return!1;return n._def.rest?e._def.rest?o(e._def.rest,n._def.rest):!1:!0}return t()}}],h=m(Z),S=(e,n)=>({name:e,rule:n});exports.createCompareFn=m;exports.createIsSameTypeFn=_;exports.defineCompareRule=S;exports.isCompatibleType=h;exports.isCompatibleTypePresetRules=Z;exports.isSameType=l;exports.isSameTypePresetRules=p;
2
2
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sources":["../src/utils.ts","../src/is-same-type.ts","../src/is-compatible-type.ts"],"sourcesContent":["import { z, type ZodType } from \"zod\";\n\nexport interface IsSameTypeOptions {\n deep: true;\n /**\n * Ignore all specific validations like min, max, length, email etc.\n *\n * You still can use `interceptor` to validate the type manually.\n */\n ignoreValidations: true;\n ignoreOptional: boolean;\n ignoreNullable: boolean;\n ignoreReadOnly: false;\n ignoreBranded: boolean;\n /**\n * A function that provides custom logic for comparing two ZodType instances.\n *\n * If the function returns `true` or `false`, the result will be used as the comparison result.\n * Otherwise, the default comparison logic will be used.\n *\n */\n interceptor: (\n a: ZodType,\n b: ZodType,\n options: IsSameTypeOptions,\n ) => boolean | void;\n}\n\nexport interface IsCompatibleTypeOptions extends IsSameTypeOptions {\n ignoreOptional: false;\n ignoreNullable: false;\n}\n\nexport const DEFAULT_COMPARE_TYPE_OPTIONS = {\n deep: true,\n ignoreOptional: false,\n ignoreNullable: false,\n ignoreReadOnly: false,\n ignoreBranded: false,\n ignoreValidations: true,\n interceptor: () => {},\n} as const satisfies IsSameTypeOptions;\n\n// See ZodFirstPartyTypeKind\nexport const isPrimitiveType = (a: ZodType): boolean => {\n return (\n a instanceof z.ZodString ||\n a instanceof z.ZodNumber ||\n a instanceof z.ZodNaN ||\n a instanceof z.ZodBigInt ||\n a instanceof z.ZodBoolean ||\n a instanceof z.ZodDate ||\n a instanceof z.ZodSymbol ||\n a instanceof z.ZodUndefined ||\n a instanceof z.ZodNull ||\n a instanceof z.ZodAny ||\n a instanceof z.ZodUnknown ||\n a instanceof z.ZodNever ||\n a instanceof z.ZodVoid\n );\n};\n\ntype Mutable<T> = { -readonly [k in keyof T]: T[k] };\n\nexport const flatUnwrapUnion = <\n T extends z.ZodUnionOptions = readonly [z.ZodTypeAny, ...z.ZodTypeAny[]],\n>(\n t: z.ZodUnion<T>,\n): Mutable<T> => {\n return t.options.flatMap((x) => {\n if (x instanceof z.ZodUnion) {\n return flatUnwrapUnion(x);\n }\n return x;\n }) as unknown as T;\n};\n","import type { EnumLike, ZodType } from \"zod\";\nimport { z } from \"zod\";\nimport {\n type IsSameTypeOptions,\n DEFAULT_COMPARE_TYPE_OPTIONS,\n isPrimitiveType,\n flatUnwrapUnion,\n} from \"./utils.ts\";\n\n/**\n * isSameType is a function that checks if two ZodTypes are the same.\n *\n * Caveats:\n * - The function does not validate specific criteria such as min or max values, length, email, etc.\n * - It excludes comparisons involving methods like .describe(), .catch(), .default(), .refine(), and .transform().\n * - When comparing definitions with .or and .and, they are assessed sequentially based on their order.\n *\n * @param a - The first ZodType to compare.\n * @param b - The second ZodType to compare.\n * @returns A boolean indicating whether the two types are the same.\n *\n * @throws Will throw an error if it encounters an unknown type.\n *\n * @example\n * ```ts\n * isSameType(z.string(), z.string()); // true\n * isSameType(z.string(), z.number()); // false\n * ```\n */\nexport const isSameType = (\n a: ZodType,\n b: ZodType,\n options?: Partial<IsSameTypeOptions>,\n): boolean => {\n const opts = { ...DEFAULT_COMPARE_TYPE_OPTIONS, ...options };\n const interceptorResult = opts.interceptor(a, b, opts);\n if (interceptorResult === true || interceptorResult === false) {\n return interceptorResult;\n }\n\n if (a === undefined || b === undefined) {\n throw new Error(\"Failed to compare type! \" + a + \" \" + b);\n }\n\n if (a === b) {\n return true;\n }\n\n // compare constructor\n if (a.constructor !== b.constructor) {\n // https://stackoverflow.com/questions/24959862/how-to-tell-if-two-javascript-instances-are-of-the-same-class-type\n return false;\n }\n // See https://github.com/colinhacks/zod/blob/master/src/types.ts\n if (!(\"typeName\" in a._def) || !(\"typeName\" in b._def)) {\n throw new Error(\"Failed to compare type! \" + a._def + \" \" + b._def);\n }\n if (a._def.typeName !== b._def.typeName) {\n return false;\n }\n\n // ZodBranded\n if (opts.ignoreBranded) {\n if (a instanceof z.ZodBranded) {\n a = a.unwrap();\n }\n if (b instanceof z.ZodBranded) {\n b = b.unwrap();\n }\n return isSameType(a, b, opts);\n } else {\n if (a instanceof z.ZodBranded || b instanceof z.ZodBranded) {\n // We can not distinguish different branded type\n // throw new Error(\"Can not distinguish different branded type!\");\n return false;\n }\n }\n\n // ZodPromise ZodOptional ZodNullable ZodBranded\n if (\"unwrap\" in a && typeof a.unwrap === \"function\") {\n if (!(\"unwrap\" in b && typeof b.unwrap === \"function\")) {\n return false;\n }\n return isSameType(a.unwrap(), b.unwrap(), opts);\n }\n\n if (!opts.ignoreOptional && a.isOptional() !== b.isOptional()) return false;\n if (!opts.ignoreNullable && a.isNullable() !== b.isNullable()) return false;\n\n if (isPrimitiveType(a)) {\n // Already assert a and b are the same constructor\n return true;\n }\n\n // ZodObject\n if (a instanceof z.ZodObject && b instanceof z.ZodObject) {\n const aShape = a.shape;\n const bShape = b.shape;\n if (Object.keys(aShape).length !== Object.keys(bShape).length) return false;\n for (const key in aShape) {\n if (!(key in bShape)) return false;\n if (!isSameType(aShape[key], bShape[key], opts)) return false;\n }\n return true;\n }\n\n // ZodArray\n if (a instanceof z.ZodArray && b instanceof z.ZodArray) {\n return isSameType(a.element, b.element, opts);\n }\n\n // ZodTuple\n if (a instanceof z.ZodTuple && b instanceof z.ZodTuple) {\n if (a.items.length !== b.items.length) return false;\n for (let i = 0; i < a.items.length; i++) {\n if (!isSameType(a.items[i], b.items[i], opts)) return false;\n }\n // Compare rest\n if (a._def.rest || b._def.rest) {\n // If one has rest, the other must have rest\n if (!a._def.rest || !b._def.rest) return false;\n return isSameType(a._def.rest, b._def.rest, opts);\n }\n return true;\n }\n\n // ZodLiteral\n if (a instanceof z.ZodLiteral && b instanceof z.ZodLiteral) {\n return a.value === b.value;\n }\n\n // ZodIntersection aka and\n if (a instanceof z.ZodIntersection && b instanceof z.ZodIntersection) {\n return (\n (isSameType(a._def.left, b._def.left, opts) &&\n isSameType(a._def.right, b._def.right, opts)) ||\n (isSameType(a._def.left, b._def.right, opts) &&\n isSameType(a._def.right, b._def.left, opts))\n );\n }\n\n // ZodUnion aka or\n if (a instanceof z.ZodUnion && b instanceof z.ZodUnion) {\n const aOptions = flatUnwrapUnion(a as z.ZodUnion<z.ZodUnionOptions>);\n const bOptions = flatUnwrapUnion(b as z.ZodUnion<z.ZodUnionOptions>);\n if (aOptions.length !== bOptions.length) return false;\n\n for (let optionA of aOptions) {\n let matchIndex = bOptions.findIndex((optionB) =>\n isSameType(optionA, optionB, opts),\n );\n if (matchIndex === -1) return false;\n bOptions.splice(matchIndex, 1);\n }\n\n return bOptions.length === 0;\n }\n\n // ZodReadonly\n if (a instanceof z.ZodReadonly && b instanceof z.ZodReadonly) {\n return isSameType(a._def.innerType, b._def.innerType, opts);\n }\n\n // ZodRecord / ZodMap\n if (\n (a instanceof z.ZodRecord && b instanceof z.ZodRecord) ||\n (a instanceof z.ZodMap && b instanceof z.ZodMap)\n ) {\n return (\n isSameType(a.keySchema, b.keySchema, opts) &&\n isSameType(a.valueSchema, b.valueSchema, opts)\n );\n }\n\n // ZodSet\n if (a instanceof z.ZodSet && b instanceof z.ZodSet) {\n return isSameType(a._def.valueType, b._def.valueType, opts);\n }\n\n // ZodFunction\n if (a instanceof z.ZodFunction && b instanceof z.ZodFunction) {\n return (\n isSameType(a.parameters(), b.parameters(), opts) &&\n isSameType(a.returnType(), b.returnType(), opts)\n );\n }\n\n // ZodEnum\n if (a instanceof z.ZodEnum && b instanceof z.ZodEnum) {\n const optionsA: [string, ...string[]] = a.options;\n const optionsB: [string, ...string[]] = b.options;\n if (optionsA.length !== optionsB.length) return false;\n for (let i = 0; i < optionsA.length; i++) {\n if (optionsA[i] !== optionsB[i]) return false;\n }\n return true;\n }\n\n // ZodNativeEnum\n if (a instanceof z.ZodNativeEnum && b instanceof z.ZodNativeEnum) {\n const enumA: EnumLike = a.enum;\n const enumB: EnumLike = b.enum;\n if (Object.keys(enumA).length !== Object.keys(enumB).length) return false;\n for (const key in enumA) {\n if (enumA[key] !== enumB[key]) return false;\n }\n return true;\n }\n\n // ZodLazy\n // ZodEffects\n // ZodDefault\n // ZodCatch\n // ZodPipeline\n // ZodTransformer\n // ZodError\n console.error('Failed to compare type! \"' + a, b);\n throw new Error(\"Unknown type! \" + a._def.typeName + \" \" + b._def.typeName);\n};\n","import { z, type ZodType } from \"zod\";\nimport { isSameType } from \"./is-same-type.ts\";\nimport {\n DEFAULT_COMPARE_TYPE_OPTIONS,\n flatUnwrapUnion,\n type IsCompatibleTypeOptions,\n} from \"./utils.ts\";\n\n/**\n * Check if a the higherType matches the lowerType\n *\n * @deprecated This a unstable API and still in development\n *\n * @param higherType The looser type\n * @param lowerType The stricter type\n *\n * @example\n * ```ts\n * isCompatibleType(z.string(), z.string()); // true\n *\n * isCompatibleType(\n * z.object({ name: z.string(), other: z.number() }),\n * z.object({ name: z.string() })\n * );\n * // true\n * ```\n */\nexport const isCompatibleType = (\n higherType: ZodType,\n lowerType: ZodType,\n options?: Partial<IsCompatibleTypeOptions>,\n): boolean => {\n const opts: IsCompatibleTypeOptions = {\n ...DEFAULT_COMPARE_TYPE_OPTIONS,\n ...options,\n };\n const interceptorResult = opts.interceptor(higherType, lowerType, opts);\n if (interceptorResult === true || interceptorResult === false) {\n return interceptorResult;\n }\n if (isSameType(higherType, lowerType, opts)) {\n return true;\n }\n\n if (!(\"typeName\" in higherType._def) || !(\"typeName\" in lowerType._def)) {\n throw new Error(\n \"Failed to compare type! \" + higherType._def + \" \" + lowerType._def,\n );\n }\n\n if (\n lowerType instanceof z.ZodOptional ||\n lowerType instanceof z.ZodNullable\n ) {\n return isCompatibleType(higherType, lowerType.unwrap(), opts);\n }\n\n // ZodUnion aka or\n if (higherType instanceof z.ZodUnion && lowerType instanceof z.ZodUnion) {\n const higherOptions = flatUnwrapUnion(higherType);\n const lowerOptions = flatUnwrapUnion(lowerType);\n for (let i = 0; i < higherOptions.length; i++) {\n const match = lowerOptions.some((option: ZodType) =>\n isCompatibleType(higherOptions[i], option, opts),\n );\n if (!match) return false;\n }\n return true;\n }\n if (higherType instanceof z.ZodUnion) {\n const higherOptions = flatUnwrapUnion(higherType);\n return higherOptions.every((option: ZodType) =>\n isCompatibleType(option, lowerType, opts),\n );\n }\n if (lowerType instanceof z.ZodUnion) {\n const lowerOptions = flatUnwrapUnion(lowerType);\n return lowerOptions.some((option: ZodType) =>\n isCompatibleType(higherType, option, opts),\n );\n }\n\n // compare constructor\n if (higherType.constructor !== lowerType.constructor) {\n return false;\n }\n\n // ZodObject\n if (higherType instanceof z.ZodObject && lowerType instanceof z.ZodObject) {\n const superTypeShape = higherType.shape;\n const subTypeShape = lowerType.shape;\n if (Object.keys(superTypeShape).length < Object.keys(subTypeShape).length)\n return false;\n for (const key in subTypeShape) {\n if (!(key in superTypeShape)) return false;\n if (!isCompatibleType(superTypeShape[key], subTypeShape[key], opts)) {\n return false;\n }\n }\n return true;\n }\n\n // ZodArray\n if (higherType instanceof z.ZodArray && lowerType instanceof z.ZodArray) {\n return isCompatibleType(higherType.element, lowerType.element, opts);\n }\n\n // ZodTuple\n if (higherType instanceof z.ZodTuple && lowerType instanceof z.ZodTuple) {\n if (higherType.items.length < lowerType.items.length) return false;\n for (let i = 0; i < lowerType.items.length; i++) {\n if (!isCompatibleType(higherType.items[i], lowerType.items[i], opts)) {\n return false;\n }\n }\n // Check rest\n if (lowerType._def.rest) {\n if (!higherType._def.rest) return false;\n return isCompatibleType(higherType._def.rest, lowerType._def.rest, opts);\n }\n return true;\n }\n\n throw new Error(\n \"Failed to compare types!\" +\n higherType._def.typeName +\n \" \" +\n lowerType._def.typeName,\n );\n};\n"],"names":["DEFAULT_COMPARE_TYPE_OPTIONS","isPrimitiveType","a","z","flatUnwrapUnion","t","x","isSameType","b","options","opts","interceptorResult","aShape","bShape","key","i","aOptions","bOptions","optionA","matchIndex","optionB","optionsA","optionsB","enumA","enumB","isCompatibleType","higherType","lowerType","higherOptions","lowerOptions","option","superTypeShape","subTypeShape"],"mappings":"uGAiCaA,EAA+B,CAC1C,KAAM,GACN,eAAgB,GAChB,eAAgB,GAChB,eAAgB,GAChB,cAAe,GACf,kBAAmB,GACnB,YAAa,IAAM,CAAC,CACtB,EAGaC,EAAmBC,GAE5BA,aAAaC,EAAE,EAAA,WACfD,aAAaC,EAAAA,EAAE,WACfD,aAAaC,IAAE,QACfD,aAAaC,IAAE,WACfD,aAAaC,EAAAA,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AU,MAAM,OAAQZ,IACtC,GAAA,CAACU,EAAiBC,EAAW,MAAMX,CAAC,EAAGY,EAAU,MAAMZ,CAAC,EAAGL,CAAI,EAC1D,MAAA,GAIP,OAAAiB,EAAU,KAAK,KACZD,EAAW,KAAK,KACdD,EAAiBC,EAAW,KAAK,KAAMC,EAAU,KAAK,KAAMjB,CAAI,EADrC,GAG7B,EACT,CAEA,MAAM,IAAI,MACR,2BACEgB,EAAW,KAAK,SAChB,IACAC,EAAU,KAAK,QAAA,CAErB"}
1
+ {"version":3,"file":"index.cjs","sources":["../src/create-compare-fn.ts","../src/utils.ts","../src/is-same-type.ts","../src/is-compatible-type.ts","../src/rules.ts"],"sourcesContent":["import type { ZodType } from \"zod\";\nimport { type CompareContext, type CompareRule } from \"./types.ts\";\n\nexport const createCompareFn = (rules: CompareRule[]) => {\n const isSameTypeFn = (\n a: ZodType,\n b: ZodType,\n context: CompareContext = {},\n ): boolean => {\n let prevIndex = -1;\n const runner = (index: number): boolean => {\n if (index === rules.length) {\n throw new Error(\"Failed to compare type! \" + a + \" \" + b);\n }\n if (index === prevIndex) {\n throw new Error(\"next() called multiple times\");\n }\n prevIndex = index;\n const rule = rules[index];\n\n if (\"stacks\" in context && Array.isArray(context.stacks)) {\n context.stacks.push({\n name: rule.name,\n target: [a, b],\n });\n }\n\n return rule.compare(\n a,\n b,\n () => runner(index + 1),\n (a, b) => isSameTypeFn(a, b, context),\n context,\n );\n };\n\n return runner(0);\n };\n return isSameTypeFn;\n};\n\n/**\n * @deprecated Use {@link createCompareFn} instead.\n */\nexport const createIsSameTypeFn = createCompareFn;\n","import { z, type ZodType } from \"zod\";\n\n// See ZodFirstPartyTypeKind\nexport const isPrimitiveType = (a: ZodType): boolean => {\n return (\n a instanceof z.ZodString ||\n a instanceof z.ZodNumber ||\n a instanceof z.ZodNaN ||\n a instanceof z.ZodBigInt ||\n a instanceof z.ZodBoolean ||\n a instanceof z.ZodDate ||\n a instanceof z.ZodSymbol ||\n a instanceof z.ZodUndefined ||\n a instanceof z.ZodNull ||\n a instanceof z.ZodAny ||\n a instanceof z.ZodUnknown ||\n a instanceof z.ZodNever ||\n a instanceof z.ZodVoid\n );\n};\n\ntype Mutable<T> = { -readonly [k in keyof T]: T[k] };\n\nexport const flatUnwrapUnion = <\n T extends z.ZodUnionOptions = readonly [z.ZodTypeAny, ...z.ZodTypeAny[]],\n>(\n t: z.ZodUnion<T>,\n): Mutable<T> => {\n return t.options.flatMap((x) => {\n if (x instanceof z.ZodUnion) {\n return flatUnwrapUnion(x);\n }\n return x;\n }) as unknown as T;\n};\n","import { z } from \"zod\";\nimport { createCompareFn } from \"./create-compare-fn.ts\";\nimport type { CompareRule } from \"./types.ts\";\nimport { flatUnwrapUnion, isPrimitiveType } from \"./utils.ts\";\n\nexport const isSameTypePresetRules = [\n {\n name: \"undefined check\",\n compare: (a, b, next) => {\n if (a === undefined || b === undefined) {\n throw new Error(\"Failed to compare type! \" + a + \" \" + b);\n }\n return next();\n },\n },\n {\n name: \"compare reference\",\n compare: (a, b, next) => {\n if (a === b) {\n return true;\n }\n return next();\n },\n },\n {\n name: \"compare constructor\",\n compare: (a, b, next) => {\n // https://stackoverflow.com/questions/24959862/how-to-tell-if-two-javascript-instances-are-of-the-same-class-type\n if (a.constructor !== b.constructor) {\n return false;\n }\n return next();\n },\n },\n {\n name: \"compare typeName\",\n compare: (a, b, next) => {\n if (!(\"typeName\" in a._def) || !(\"typeName\" in b._def)) {\n throw new Error(\"Failed to compare type! \" + a._def + \" \" + b._def);\n }\n if (a._def.typeName !== b._def.typeName) {\n return false;\n }\n return next();\n },\n },\n {\n name: \"compare ZodBranded\",\n compare: (a, b, next) => {\n if (a instanceof z.ZodBranded || b instanceof z.ZodBranded) {\n // We can not distinguish different branded type\n // throw new Error(\"Can not distinguish different branded type!\");\n return false;\n }\n return next();\n },\n },\n {\n name: \"unwrap ZodType\",\n // ZodPromise ZodOptional ZodNullable ZodBranded\n compare: (a, b, next, recheck) => {\n if (\"unwrap\" in a && typeof a.unwrap === \"function\") {\n if (!(\"unwrap\" in b && typeof b.unwrap === \"function\")) {\n return false;\n }\n return recheck(a.unwrap(), b.unwrap());\n }\n return next();\n },\n },\n {\n name: \"is same primitive\",\n compare: (a, b, next) => {\n if (\n isPrimitiveType(a) &&\n isPrimitiveType(b) &&\n a.constructor === b.constructor\n ) {\n return true;\n }\n return next();\n },\n },\n {\n name: \"compare ZodObject\",\n compare: (a, b, next, recheck) => {\n if (a instanceof z.ZodObject && b instanceof z.ZodObject) {\n const aShape = a.shape;\n const bShape = b.shape;\n if (Object.keys(aShape).length !== Object.keys(bShape).length)\n return false;\n for (const key in aShape) {\n if (!(key in bShape)) return false;\n if (!recheck(aShape[key], bShape[key])) return false;\n }\n return true;\n }\n return next();\n },\n },\n {\n name: \"compare ZodArray\",\n compare: (a, b, next, recheck) => {\n if (a instanceof z.ZodArray && b instanceof z.ZodArray) {\n return recheck(a.element, b.element);\n }\n return next();\n },\n },\n {\n name: \"compare ZodTuple\",\n compare: (a, b, next, recheck) => {\n if (a instanceof z.ZodTuple && b instanceof z.ZodTuple) {\n if (a.items.length !== b.items.length) return false;\n for (let i = 0; i < a.items.length; i++) {\n if (!recheck(a.items[i], b.items[i])) return false;\n }\n // Compare rest\n if (a._def.rest || b._def.rest) {\n // If one has rest, the other must have rest\n if (!a._def.rest || !b._def.rest) return false;\n return recheck(a._def.rest, b._def.rest);\n }\n return true;\n }\n return next();\n },\n },\n {\n name: \"compare ZodLiteral\",\n compare: (a, b, next) => {\n if (a instanceof z.ZodLiteral && b instanceof z.ZodLiteral) {\n return a.value === b.value;\n }\n return next();\n },\n },\n {\n name: \"compare ZodIntersection\",\n compare: (a, b, next, recheck) => {\n // ZodIntersection aka and\n if (a instanceof z.ZodIntersection && b instanceof z.ZodIntersection) {\n return (\n (recheck(a._def.left, b._def.left) &&\n recheck(a._def.right, b._def.right)) ||\n (recheck(a._def.left, b._def.right) &&\n recheck(a._def.right, b._def.left))\n );\n }\n return next();\n },\n },\n {\n name: \"compare ZodUnion\",\n compare: (a, b, next, recheck) => {\n // ZodUnion aka or\n if (a instanceof z.ZodUnion && b instanceof z.ZodUnion) {\n const aOptions = flatUnwrapUnion(a as z.ZodUnion<z.ZodUnionOptions>);\n const bOptions = flatUnwrapUnion(b as z.ZodUnion<z.ZodUnionOptions>);\n if (aOptions.length !== bOptions.length) return false;\n for (let optionA of aOptions) {\n let matchIndex = bOptions.findIndex((optionB) =>\n recheck(optionA, optionB),\n );\n if (matchIndex === -1) return false;\n bOptions.splice(matchIndex, 1);\n }\n return bOptions.length === 0;\n }\n return next();\n },\n },\n {\n name: \"compare ZodReadonly\",\n compare: (a, b, next, recheck) => {\n if (a instanceof z.ZodReadonly && b instanceof z.ZodReadonly) {\n return recheck(a._def.innerType, b._def.innerType);\n }\n return next();\n },\n },\n {\n name: \"compare ZodRecord\",\n compare: (a, b, next, recheck) => {\n if (a instanceof z.ZodRecord && b instanceof z.ZodRecord) {\n return (\n recheck(a.keySchema, b.keySchema) &&\n recheck(a.valueSchema, b.valueSchema)\n );\n }\n return next();\n },\n },\n {\n name: \"compare ZodMap\",\n compare: (a, b, next, recheck) => {\n if (a instanceof z.ZodMap && b instanceof z.ZodMap) {\n return (\n recheck(a.keySchema, b.keySchema) &&\n recheck(a.valueSchema, b.valueSchema)\n );\n }\n return next();\n },\n },\n {\n name: \"compare ZodSet\",\n compare: (a, b, next, recheck) => {\n if (a instanceof z.ZodSet && b instanceof z.ZodSet) {\n return recheck(a._def.valueType, b._def.valueType);\n }\n return next();\n },\n },\n {\n name: \"compare ZodFunction\",\n compare: (a, b, next, recheck) => {\n if (a instanceof z.ZodFunction && b instanceof z.ZodFunction) {\n return (\n recheck(a.parameters(), b.parameters()) &&\n recheck(a.returnType(), b.returnType())\n );\n }\n return next();\n },\n },\n {\n name: \"compare ZodEnum\",\n compare: (a, b, next) => {\n if (a instanceof z.ZodEnum && b instanceof z.ZodEnum) {\n const optionsA: [string, ...string[]] = a.options;\n const optionsB: [string, ...string[]] = b.options;\n if (optionsA.length !== optionsB.length) return false;\n for (let i = 0; i < optionsA.length; i++) {\n if (optionsA[i] !== optionsB[i]) return false;\n }\n return true;\n }\n return next();\n },\n },\n {\n name: \"compare ZodNativeEnum\",\n compare: (a, b, next) => {\n if (a instanceof z.ZodNativeEnum && b instanceof z.ZodNativeEnum) {\n const enumA: z.EnumLike = a.enum;\n const enumB: z.EnumLike = b.enum;\n if (Object.keys(enumA).length !== Object.keys(enumB).length)\n return false;\n for (const key in enumA) {\n if (enumA[key] !== enumB[key]) return false;\n }\n return true;\n }\n return next();\n },\n },\n] as const satisfies CompareRule[];\n\nexport const strictIsSameTypeRules = [\n {\n name: \"compare optional\",\n compare: (a, b, next) => {\n if (a.isOptional() !== b.isOptional()) return false;\n return next();\n },\n },\n {\n name: \"compare nullable\",\n compare: (a, b, next) => {\n if (a.isNullable() !== b.isNullable()) return false;\n return next();\n },\n },\n {\n name: \"compare corece\",\n compare: (a, b, next) => {\n if (\"corece\" in a._def && \"corece\" in b._def) {\n if (a._def.corece !== b._def.corece) {\n return false;\n }\n }\n if (\"corece\" in a._def) {\n return false;\n }\n if (\"corece\" in b._def) {\n return false;\n }\n return next();\n },\n },\n {\n name: \"compare description\",\n compare: (a, b, next) => {\n if (a.description !== b.description) {\n return false;\n }\n return next();\n },\n },\n] as const satisfies CompareRule[];\n\n/**\n * isSameType is a function that checks if two ZodTypes are the same.\n *\n * Caveats:\n * - The function does not validate specific criteria such as min or max values, length, email, etc.\n * - It excludes comparisons involving methods like .describe(), .catch(), .default(), .refine(), and .transform().\n *\n * @param a - The first ZodType to compare.\n * @param b - The second ZodType to compare.\n * @returns A boolean indicating whether the two types are the same.\n *\n * @throws Will throw an error if it encounters an unknown type.\n *\n * @example\n * ```ts\n * isSameType(z.string(), z.string()); // true\n * isSameType(z.string(), z.number()); // false\n * ```\n */\nexport const isSameType = createCompareFn(isSameTypePresetRules);\n","import { z, type ZodType } from \"zod\";\nimport { createCompareFn } from \"./create-compare-fn.ts\";\nimport { isSameType } from \"./is-same-type.ts\";\nimport type { CompareRule } from \"./types.ts\";\nimport { flatUnwrapUnion } from \"./utils.ts\";\n\nexport const isCompatibleTypePresetRules = [\n {\n name: \"is same type\",\n compare: (higherType, lowerType, next) => {\n if (isSameType(higherType, lowerType)) {\n return true;\n }\n return next();\n },\n },\n {\n name: \"check typeName\",\n compare: (higherType, lowerType, next) => {\n if (!(\"typeName\" in higherType._def) || !(\"typeName\" in lowerType._def)) {\n throw new Error(\n \"Failed to compare type! \" + higherType._def + \" \" + lowerType._def,\n );\n }\n return next();\n },\n },\n {\n name: \"check ZodOptional/ZodNullable\",\n compare: (higherType, lowerType, next, recheck) => {\n if (\n lowerType instanceof z.ZodOptional ||\n lowerType instanceof z.ZodNullable\n ) {\n return recheck(higherType, lowerType.unwrap());\n }\n return next();\n },\n },\n {\n name: \"check ZodUnion\",\n compare: (higherType, lowerType, next, recheck) => {\n if (higherType instanceof z.ZodUnion && lowerType instanceof z.ZodUnion) {\n const higherOptions = flatUnwrapUnion(higherType);\n const lowerOptions = flatUnwrapUnion(lowerType);\n for (let i = 0; i < higherOptions.length; i++) {\n const match = lowerOptions.some((option: ZodType) =>\n recheck(higherOptions[i], option),\n );\n if (!match) return false;\n }\n return true;\n }\n if (higherType instanceof z.ZodUnion) {\n const higherOptions = flatUnwrapUnion(higherType);\n return higherOptions.every((option: ZodType) =>\n recheck(option, lowerType),\n );\n }\n if (lowerType instanceof z.ZodUnion) {\n const lowerOptions = flatUnwrapUnion(lowerType);\n return lowerOptions.some((option: ZodType) =>\n recheck(higherType, option),\n );\n }\n return next();\n },\n },\n {\n name: \"compare constructor\",\n compare: (higherType, lowerType, next) => {\n // We have already checked if the types are ZodUnion or ZodOptional/ZodNullable before\n if (higherType.constructor !== lowerType.constructor) {\n return false;\n }\n return next();\n },\n },\n {\n name: \"check ZodObject\",\n compare: (higherType, lowerType, next, recheck) => {\n if (\n higherType instanceof z.ZodObject &&\n lowerType instanceof z.ZodObject\n ) {\n const superTypeShape = higherType.shape;\n const subTypeShape = lowerType.shape;\n if (\n Object.keys(superTypeShape).length < Object.keys(subTypeShape).length\n )\n return false;\n for (const key in subTypeShape) {\n if (!(key in superTypeShape)) return false;\n if (!recheck(superTypeShape[key], subTypeShape[key])) {\n return false;\n }\n }\n return true;\n }\n return next();\n },\n },\n {\n name: \"check ZodArray\",\n compare: (higherType, lowerType, next, recheck) => {\n if (higherType instanceof z.ZodArray && lowerType instanceof z.ZodArray) {\n return recheck(higherType.element, lowerType.element);\n }\n return next();\n },\n },\n {\n name: \"check ZodTuple\",\n compare: (higherType, lowerType, next, recheck) => {\n if (higherType instanceof z.ZodTuple && lowerType instanceof z.ZodTuple) {\n if (higherType.items.length < lowerType.items.length) return false;\n for (let i = 0; i < lowerType.items.length; i++) {\n if (!recheck(higherType.items[i], lowerType.items[i])) {\n return false;\n }\n }\n // Check rest\n if (lowerType._def.rest) {\n if (!higherType._def.rest) return false;\n return recheck(higherType._def.rest, lowerType._def.rest);\n }\n return true;\n }\n return next();\n },\n },\n] as const satisfies CompareRule[];\n\n/**\n * Check if a the higherType matches the lowerType\n *\n * @deprecated This a unstable API and still in development\n *\n * @param higherType The looser type\n * @param lowerType The stricter type\n *\n * @example\n * ```ts\n * isCompatibleType(z.string(), z.string()); // true\n *\n * isCompatibleType(\n * z.object({ name: z.string(), other: z.number() }),\n * z.object({ name: z.string() })\n * );\n * // true\n * ```\n */\nexport const isCompatibleType = createCompareFn(isCompatibleTypePresetRules);\n","import type { CompareFn } from \"./types.ts\";\n\nexport const defineCompareRule = (name: string, rule: CompareFn) => ({\n name,\n rule,\n});\n"],"names":["createCompareFn","rules","isSameTypeFn","a","b","context","prevIndex","runner","index","rule","createIsSameTypeFn","isPrimitiveType","z","flatUnwrapUnion","t","x","isSameTypePresetRules","next","recheck","aShape","bShape","key","i","aOptions","bOptions","optionA","matchIndex","optionB","optionsA","optionsB","enumA","enumB","isSameType","isCompatibleTypePresetRules","higherType","lowerType","higherOptions","lowerOptions","option","superTypeShape","subTypeShape","isCompatibleType","defineCompareRule","name"],"mappings":"uGAGaA,EAAmBC,GAAyB,CACvD,MAAMC,EAAe,CACnBC,EACAC,EACAC,EAA0B,CAAA,IACd,CACZ,IAAIC,EAAY,GACV,MAAAC,EAAUC,GAA2B,CACrC,GAAAA,IAAUP,EAAM,OAClB,MAAM,IAAI,MAAM,2BAA6BE,EAAI,IAAMC,CAAC,EAE1D,GAAII,IAAUF,EACN,MAAA,IAAI,MAAM,8BAA8B,EAEpCA,EAAAE,EACN,MAAAC,EAAOR,EAAMO,CAAK,EAExB,MAAI,WAAYH,GAAW,MAAM,QAAQA,EAAQ,MAAM,GACrDA,EAAQ,OAAO,KAAK,CAClB,KAAMI,EAAK,KACX,OAAQ,CAACN,EAAGC,CAAC,CAAA,CACd,EAGIK,EAAK,QACVN,EACAC,EACA,IAAMG,EAAOC,EAAQ,CAAC,EACtB,CAACL,EAAGC,IAAMF,EAAaC,EAAGC,EAAGC,CAAO,EACpCA,CAAA,CACF,EAGF,OAAOE,EAAO,CAAC,CAAA,EAEV,OAAAL,CACT,EAKaQ,EAAqBV,ECzCrBW,EAAmBR,GAE5BA,aAAaS,EAAE,EAAA,WACfT,aAAaS,EAAAA,EAAE,WACfT,aAAaS,IAAE,QACfT,aAAaS,IAAE,WACfT,aAAaS,EAAAA,EAAE,YACfT,aAAaS,IAAE,SACfT,aAAaS,EAAA,EAAE,WACfT,aAAaS,EAAAA,EAAE,cACfT,aAAaS,IAAE,SACfT,aAAaS,EAAE,EAAA,QACfT,aAAaS,EAAAA,EAAE,YACfT,aAAaS,EAAE,EAAA,UACfT,aAAaS,EAAE,EAAA,QAMNC,EAGXC,GAEOA,EAAE,QAAQ,QAASC,GACpBA,aAAaH,IAAE,SACVC,EAAgBE,CAAC,EAEnBA,CACR,EC5BUC,EAAwB,CACnC,CACE,KAAM,kBACN,QAAS,CAACb,EAAGC,EAAGa,IAAS,CACnB,GAAAd,IAAM,QAAaC,IAAM,O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@ -1,5 +1,33 @@
1
+ import { CompareContext as CompareContext_2 } from './types.ts';
2
+ import { z } from 'zod';
1
3
  import { ZodType } from 'zod';
2
4
 
5
+ export declare type CompareContext = {
6
+ stacks?: {
7
+ name: string;
8
+ target: [a: ZodType, b: ZodType];
9
+ }[];
10
+ } & Record<string, unknown>;
11
+
12
+ export declare type CompareFn = (a: ZodType, b: ZodType, next: () => boolean, recheck: (a: ZodType, b: ZodType) => boolean, context: CompareContext) => boolean;
13
+
14
+ export declare type CompareRule = {
15
+ name: string;
16
+ compare: CompareFn;
17
+ };
18
+
19
+ export declare const createCompareFn: (rules: CompareRule[]) => (a: ZodType, b: ZodType, context?: CompareContext) => boolean;
20
+
21
+ /**
22
+ * @deprecated Use {@link createCompareFn} instead.
23
+ */
24
+ export declare const createIsSameTypeFn: (rules: CompareRule[]) => (a: ZodType, b: ZodType, context?: CompareContext) => boolean;
25
+
26
+ export declare const defineCompareRule: (name: string, rule: CompareFn) => {
27
+ name: string;
28
+ rule: CompareFn;
29
+ };
30
+
3
31
  /**
4
32
  * Check if a the higherType matches the lowerType
5
33
  *
@@ -19,12 +47,33 @@ import { ZodType } from 'zod';
19
47
  * // true
20
48
  * ```
21
49
  */
22
- export declare const isCompatibleType: (higherType: ZodType, lowerType: ZodType, options?: Partial<IsCompatibleTypeOptions>) => boolean;
50
+ export declare const isCompatibleType: (a: z.ZodType<any, z.ZodTypeDef, any>, b: z.ZodType<any, z.ZodTypeDef, any>, context?: CompareContext_2) => boolean;
23
51
 
24
- declare interface IsCompatibleTypeOptions extends IsSameTypeOptions {
25
- ignoreOptional: false;
26
- ignoreNullable: false;
27
- }
52
+ export declare const isCompatibleTypePresetRules: [{
53
+ readonly name: "is same type";
54
+ readonly compare: (higherType: z.ZodType<any, z.ZodTypeDef, any>, lowerType: z.ZodType<any, z.ZodTypeDef, any>, next: () => boolean) => boolean;
55
+ }, {
56
+ readonly name: "check typeName";
57
+ readonly compare: (higherType: z.ZodType<any, z.ZodTypeDef, any>, lowerType: z.ZodType<any, z.ZodTypeDef, any>, next: () => boolean) => boolean;
58
+ }, {
59
+ readonly name: "check ZodOptional/ZodNullable";
60
+ readonly compare: (higherType: z.ZodType<any, z.ZodTypeDef, any>, lowerType: z.ZodType<any, z.ZodTypeDef, any>, next: () => boolean, recheck: (a: z.ZodType<any, z.ZodTypeDef, any>, b: z.ZodType<any, z.ZodTypeDef, any>) => boolean) => boolean;
61
+ }, {
62
+ readonly name: "check ZodUnion";
63
+ readonly compare: (higherType: z.ZodType<any, z.ZodTypeDef, any>, lowerType: z.ZodType<any, z.ZodTypeDef, any>, next: () => boolean, recheck: (a: z.ZodType<any, z.ZodTypeDef, any>, b: z.ZodType<any, z.ZodTypeDef, any>) => boolean) => any;
64
+ }, {
65
+ readonly name: "compare constructor";
66
+ readonly compare: (higherType: z.ZodType<any, z.ZodTypeDef, any>, lowerType: z.ZodType<any, z.ZodTypeDef, any>, next: () => boolean) => boolean;
67
+ }, {
68
+ readonly name: "check ZodObject";
69
+ readonly compare: (higherType: z.ZodType<any, z.ZodTypeDef, any>, lowerType: z.ZodType<any, z.ZodTypeDef, any>, next: () => boolean, recheck: (a: z.ZodType<any, z.ZodTypeDef, any>, b: z.ZodType<any, z.ZodTypeDef, any>) => boolean) => boolean;
70
+ }, {
71
+ readonly name: "check ZodArray";
72
+ readonly compare: (higherType: z.ZodType<any, z.ZodTypeDef, any>, lowerType: z.ZodType<any, z.ZodTypeDef, any>, next: () => boolean, recheck: (a: z.ZodType<any, z.ZodTypeDef, any>, b: z.ZodType<any, z.ZodTypeDef, any>) => boolean) => boolean;
73
+ }, {
74
+ readonly name: "check ZodTuple";
75
+ readonly compare: (higherType: z.ZodType<any, z.ZodTypeDef, any>, lowerType: z.ZodType<any, z.ZodTypeDef, any>, next: () => boolean, recheck: (a: z.ZodType<any, z.ZodTypeDef, any>, b: z.ZodType<any, z.ZodTypeDef, any>) => boolean) => boolean;
76
+ }];
28
77
 
29
78
  /**
30
79
  * isSameType is a function that checks if two ZodTypes are the same.
@@ -32,7 +81,6 @@ declare interface IsCompatibleTypeOptions extends IsSameTypeOptions {
32
81
  * Caveats:
33
82
  * - The function does not validate specific criteria such as min or max values, length, email, etc.
34
83
  * - It excludes comparisons involving methods like .describe(), .catch(), .default(), .refine(), and .transform().
35
- * - When comparing definitions with .or and .and, they are assessed sequentially based on their order.
36
84
  *
37
85
  * @param a - The first ZodType to compare.
38
86
  * @param b - The second ZodType to compare.
@@ -46,28 +94,68 @@ declare interface IsCompatibleTypeOptions extends IsSameTypeOptions {
46
94
  * isSameType(z.string(), z.number()); // false
47
95
  * ```
48
96
  */
49
- export declare const isSameType: (a: ZodType, b: ZodType, options?: Partial<IsSameTypeOptions>) => boolean;
97
+ export declare const isSameType: (a: z.ZodType<any, z.ZodTypeDef, any>, b: z.ZodType<any, z.ZodTypeDef, any>, context?: CompareContext_2) => boolean;
50
98
 
51
- export declare interface IsSameTypeOptions {
52
- deep: true;
53
- /**
54
- * Ignore all specific validations like min, max, length, email etc.
55
- *
56
- * You still can use `interceptor` to validate the type manually.
57
- */
58
- ignoreValidations: true;
59
- ignoreOptional: boolean;
60
- ignoreNullable: boolean;
61
- ignoreReadOnly: false;
62
- ignoreBranded: boolean;
63
- /**
64
- * A function that provides custom logic for comparing two ZodType instances.
65
- *
66
- * If the function returns `true` or `false`, the result will be used as the comparison result.
67
- * Otherwise, the default comparison logic will be used.
68
- *
69
- */
70
- interceptor: (a: ZodType, b: ZodType, options: IsSameTypeOptions) => boolean | void;
71
- }
99
+ export declare const isSameTypePresetRules: [{
100
+ readonly name: "undefined check";
101
+ readonly compare: (a: z.ZodType<any, z.ZodTypeDef, any>, b: z.ZodType<any, z.ZodTypeDef, any>, next: () => boolean) => boolean;
102
+ }, {
103
+ readonly name: "compare reference";
104
+ readonly compare: (a: z.ZodType<any, z.ZodTypeDef, any>, b: z.ZodType<any, z.ZodTypeDef, any>, next: () => boolean) => boolean;
105
+ }, {
106
+ readonly name: "compare constructor";
107
+ readonly compare: (a: z.ZodType<any, z.ZodTypeDef, any>, b: z.ZodType<any, z.ZodTypeDef, any>, next: () => boolean) => boolean;
108
+ }, {
109
+ readonly name: "compare typeName";
110
+ readonly compare: (a: z.ZodType<any, z.ZodTypeDef, any>, b: z.ZodType<any, z.ZodTypeDef, any>, next: () => boolean) => boolean;
111
+ }, {
112
+ readonly name: "compare ZodBranded";
113
+ readonly compare: (a: z.ZodType<any, z.ZodTypeDef, any>, b: z.ZodType<any, z.ZodTypeDef, any>, next: () => boolean) => boolean;
114
+ }, {
115
+ readonly name: "unwrap ZodType";
116
+ readonly compare: (a: z.ZodType<any, z.ZodTypeDef, any>, b: z.ZodType<any, z.ZodTypeDef, any>, next: () => boolean, recheck: (a: z.ZodType<any, z.ZodTypeDef, any>, b: z.ZodType<any, z.ZodTypeDef, any>) => boolean) => boolean;
117
+ }, {
118
+ readonly name: "is same primitive";
119
+ readonly compare: (a: z.ZodType<any, z.ZodTypeDef, any>, b: z.ZodType<any, z.ZodTypeDef, any>, next: () => boolean) => boolean;
120
+ }, {
121
+ readonly name: "compare ZodObject";
122
+ readonly compare: (a: z.ZodType<any, z.ZodTypeDef, any>, b: z.ZodType<any, z.ZodTypeDef, any>, next: () => boolean, recheck: (a: z.ZodType<any, z.ZodTypeDef, any>, b: z.ZodType<any, z.ZodTypeDef, any>) => boolean) => boolean;
123
+ }, {
124
+ readonly name: "compare ZodArray";
125
+ readonly compare: (a: z.ZodType<any, z.ZodTypeDef, any>, b: z.ZodType<any, z.ZodTypeDef, any>, next: () => boolean, recheck: (a: z.ZodType<any, z.ZodTypeDef, any>, b: z.ZodType<any, z.ZodTypeDef, any>) => boolean) => boolean;
126
+ }, {
127
+ readonly name: "compare ZodTuple";
128
+ readonly compare: (a: z.ZodType<any, z.ZodTypeDef, any>, b: z.ZodType<any, z.ZodTypeDef, any>, next: () => boolean, recheck: (a: z.ZodType<any, z.ZodTypeDef, any>, b: z.ZodType<any, z.ZodTypeDef, any>) => boolean) => boolean;
129
+ }, {
130
+ readonly name: "compare ZodLiteral";
131
+ readonly compare: (a: z.ZodType<any, z.ZodTypeDef, any>, b: z.ZodType<any, z.ZodTypeDef, any>, next: () => boolean) => boolean;
132
+ }, {
133
+ readonly name: "compare ZodIntersection";
134
+ readonly compare: (a: z.ZodType<any, z.ZodTypeDef, any>, b: z.ZodType<any, z.ZodTypeDef, any>, next: () => boolean, recheck: (a: z.ZodType<any, z.ZodTypeDef, any>, b: z.ZodType<any, z.ZodTypeDef, any>) => boolean) => boolean;
135
+ }, {
136
+ readonly name: "compare ZodUnion";
137
+ readonly compare: (a: z.ZodType<any, z.ZodTypeDef, any>, b: z.ZodType<any, z.ZodTypeDef, any>, next: () => boolean, recheck: (a: z.ZodType<any, z.ZodTypeDef, any>, b: z.ZodType<any, z.ZodTypeDef, any>) => boolean) => boolean;
138
+ }, {
139
+ readonly name: "compare ZodReadonly";
140
+ readonly compare: (a: z.ZodType<any, z.ZodTypeDef, any>, b: z.ZodType<any, z.ZodTypeDef, any>, next: () => boolean, recheck: (a: z.ZodType<any, z.ZodTypeDef, any>, b: z.ZodType<any, z.ZodTypeDef, any>) => boolean) => boolean;
141
+ }, {
142
+ readonly name: "compare ZodRecord";
143
+ readonly compare: (a: z.ZodType<any, z.ZodTypeDef, any>, b: z.ZodType<any, z.ZodTypeDef, any>, next: () => boolean, recheck: (a: z.ZodType<any, z.ZodTypeDef, any>, b: z.ZodType<any, z.ZodTypeDef, any>) => boolean) => boolean;
144
+ }, {
145
+ readonly name: "compare ZodMap";
146
+ readonly compare: (a: z.ZodType<any, z.ZodTypeDef, any>, b: z.ZodType<any, z.ZodTypeDef, any>, next: () => boolean, recheck: (a: z.ZodType<any, z.ZodTypeDef, any>, b: z.ZodType<any, z.ZodTypeDef, any>) => boolean) => boolean;
147
+ }, {
148
+ readonly name: "compare ZodSet";
149
+ readonly compare: (a: z.ZodType<any, z.ZodTypeDef, any>, b: z.ZodType<any, z.ZodTypeDef, any>, next: () => boolean, recheck: (a: z.ZodType<any, z.ZodTypeDef, any>, b: z.ZodType<any, z.ZodTypeDef, any>) => boolean) => boolean;
150
+ }, {
151
+ readonly name: "compare ZodFunction";
152
+ readonly compare: (a: z.ZodType<any, z.ZodTypeDef, any>, b: z.ZodType<any, z.ZodTypeDef, any>, next: () => boolean, recheck: (a: z.ZodType<any, z.ZodTypeDef, any>, b: z.ZodType<any, z.ZodTypeDef, any>) => boolean) => boolean;
153
+ }, {
154
+ readonly name: "compare ZodEnum";
155
+ readonly compare: (a: z.ZodType<any, z.ZodTypeDef, any>, b: z.ZodType<any, z.ZodTypeDef, any>, next: () => boolean) => boolean;
156
+ }, {
157
+ readonly name: "compare ZodNativeEnum";
158
+ readonly compare: (a: z.ZodType<any, z.ZodTypeDef, any>, b: z.ZodType<any, z.ZodTypeDef, any>, next: () => boolean) => boolean;
159
+ }];
72
160
 
73
161
  export { }