zod-compare 0.1.1 → 0.1.3

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 whitewater
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,6 +1,7 @@
1
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
+ [![npm](https://img.shields.io/npm/v/zod-compare)](https://www.npmjs.com/package/zod-compare)
4
5
 
5
6
  Compare two [Zod](https://zod.dev/) schemas recursively.
6
7
 
package/dist/index.cjs CHANGED
@@ -1,2 +1,2 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const t=require("zod"),a={deep:!0,ignoreOptional:!1,ignoreNullable:!1,ignoreReadOnly:!1,ignoreBranded:!1,ignoreValidations:!0,ignoreIntersectionsOrder:!1,interceptor:()=>{}},l=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,f=(e,n,d)=>{const r={...a,...d},u=r.interceptor(e,n,r);if(u===!0||u===!1)return u;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(r.ignoreBranded)return e instanceof t.z.ZodBranded&&(e=e.unwrap()),n instanceof t.z.ZodBranded&&(n=n.unwrap()),f(e,n,r);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"?f(e.unwrap(),n.unwrap(),r):!1;if(!r.ignoreOptional&&e.isOptional()!==n.isOptional()||!r.ignoreNullable&&e.isNullable()!==n.isNullable())return!1;if(l(e))return!0;if(e instanceof t.z.ZodObject&&n instanceof t.z.ZodObject){const o=e.shape,s=n.shape;if(Object.keys(o).length!==Object.keys(s).length)return!1;for(const i in o)if(!(i in s)||!f(o[i],s[i],r))return!1;return!0}if(e instanceof t.z.ZodArray&&n instanceof t.z.ZodArray)return f(e.element,n.element,r);if(e instanceof t.z.ZodTuple&&n instanceof t.z.ZodTuple){if(e.items.length!==n.items.length)return!1;for(let o=0;o<e.items.length;o++)if(!f(e.items[o],n.items[o],r))return!1;return e._def.rest||n._def.rest?!e._def.rest||!n._def.rest?!1:f(e._def.rest,n._def.rest,r):!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 f(e._def.left,n._def.left,r)&&f(e._def.right,n._def.right,r);if(e instanceof t.z.ZodUnion&&n instanceof t.z.ZodUnion){if(e.options.length!==n.options.length)return!1;for(let o=0;o<e.options.length;o++)if(!f(e.options[o],n.options[o],r))return!1;return!0}if(e instanceof t.z.ZodReadonly&&n instanceof t.z.ZodReadonly)return f(e._def.innerType,n._def.innerType,r);if(e instanceof t.z.ZodRecord&&n instanceof t.z.ZodRecord||e instanceof t.z.ZodMap&&n instanceof t.z.ZodMap)return f(e.keySchema,n.keySchema,r)&&f(e.valueSchema,n.valueSchema,r);if(e instanceof t.z.ZodSet&&n instanceof t.z.ZodSet)return f(e._def.valueType,n._def.valueType,r);if(e instanceof t.z.ZodFunction&&n instanceof t.z.ZodFunction)return f(e.parameters(),n.parameters(),r)&&f(e.returnType(),n.returnType(),r);if(e instanceof t.z.ZodEnum&&n instanceof t.z.ZodEnum){const o=e.options,s=n.options;if(o.length!==s.length)return!1;for(let i=0;i<o.length;i++)if(o[i]!==s[i])return!1;return!0}if(e instanceof t.z.ZodNativeEnum&&n instanceof t.z.ZodNativeEnum){const o=e.enum,s=n.enum;if(Object.keys(o).length!==Object.keys(s).length)return!1;for(const i in o)if(o[i]!==s[i])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,d)=>{const r={...a,...d},u=r.interceptor(e,n,r);if(u===!0||u===!1)return u;if(f(e,n,r))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(),r);if(e instanceof t.z.ZodUnion&&n instanceof t.z.ZodUnion){for(let o=0;o<e.options.length;o++)if(!n.options.some(i=>c(i,n.options[o],r)))return!1;return!0}if(e instanceof t.z.ZodUnion)return e.options.every(o=>c(o,n,r));if(n instanceof t.z.ZodUnion)return n.options.some(o=>c(e,o,r));if(e.constructor!==n.constructor)return!1;if(e instanceof t.z.ZodObject&&n instanceof t.z.ZodObject){const o=e.shape,s=n.shape;if(Object.keys(o).length<Object.keys(s).length)return!1;for(const i in s)if(!(i in o)||!c(o[i],s[i],r))return!1;return!0}if(e instanceof t.z.ZodArray&&n instanceof t.z.ZodArray)return c(e.element,n.element,r);if(e instanceof t.z.ZodTuple&&n instanceof t.z.ZodTuple){if(e.items.length<n.items.length)return!1;for(let o=0;o<n.items.length;o++)if(!c(e.items[o],n.items[o],r))return!1;return n._def.rest?e._def.rest?c(e._def.rest,n._def.rest,r):!1:!0}throw new Error("Failed to compare types!"+e._def.typeName+" "+n._def.typeName)};exports.isCompatibleType=c;exports.isSameType=f;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const t=require("zod"),z={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,a=e=>e.options.flatMap(n=>n instanceof t.z.ZodUnion?a(n):n),s=(e,n,u)=>{const r={...z,...u},d=r.interceptor(e,n,r);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(r.ignoreBranded)return e instanceof t.z.ZodBranded&&(e=e.unwrap()),n instanceof t.z.ZodBranded&&(n=n.unwrap()),s(e,n,r);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(),r):!1;if(!r.ignoreOptional&&e.isOptional()!==n.isOptional()||!r.ignoreNullable&&e.isNullable()!==n.isNullable())return!1;if(Z(e))return!0;if(e instanceof t.z.ZodObject&&n instanceof t.z.ZodObject){const o=e.shape,i=n.shape;if(Object.keys(o).length!==Object.keys(i).length)return!1;for(const f in o)if(!(f in i)||!s(o[f],i[f],r))return!1;return!0}if(e instanceof t.z.ZodArray&&n instanceof t.z.ZodArray)return s(e.element,n.element,r);if(e instanceof t.z.ZodTuple&&n instanceof t.z.ZodTuple){if(e.items.length!==n.items.length)return!1;for(let o=0;o<e.items.length;o++)if(!s(e.items[o],n.items[o],r))return!1;return e._def.rest||n._def.rest?!e._def.rest||!n._def.rest?!1:s(e._def.rest,n._def.rest,r):!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,r)&&s(e._def.right,n._def.right,r)||s(e._def.left,n._def.right,r)&&s(e._def.right,n._def.left,r);if(e instanceof t.z.ZodUnion&&n instanceof t.z.ZodUnion){const o=a(e),i=a(n);if(o.length!==i.length)return!1;for(let f of o){let l=i.findIndex(m=>s(f,m,r));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,r);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,r)&&s(e.valueSchema,n.valueSchema,r);if(e instanceof t.z.ZodSet&&n instanceof t.z.ZodSet)return s(e._def.valueType,n._def.valueType,r);if(e instanceof t.z.ZodFunction&&n instanceof t.z.ZodFunction)return s(e.parameters(),n.parameters(),r)&&s(e.returnType(),n.returnType(),r);if(e instanceof t.z.ZodEnum&&n instanceof t.z.ZodEnum){const o=e.options,i=n.options;if(o.length!==i.length)return!1;for(let f=0;f<o.length;f++)if(o[f]!==i[f])return!1;return!0}if(e instanceof t.z.ZodNativeEnum&&n instanceof t.z.ZodNativeEnum){const o=e.enum,i=n.enum;if(Object.keys(o).length!==Object.keys(i).length)return!1;for(const f in o)if(o[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,u)=>{const r={...z,...u},d=r.interceptor(e,n,r);if(d===!0||d===!1)return d;if(s(e,n,r))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(),r);if(e instanceof t.z.ZodUnion&&n instanceof t.z.ZodUnion){for(let o=0;o<e.options.length;o++)if(!n.options.some(f=>c(f,n.options[o],r)))return!1;return!0}if(e instanceof t.z.ZodUnion)return e.options.every(o=>c(o,n,r));if(n instanceof t.z.ZodUnion)return n.options.some(o=>c(e,o,r));if(e.constructor!==n.constructor)return!1;if(e instanceof t.z.ZodObject&&n instanceof t.z.ZodObject){const o=e.shape,i=n.shape;if(Object.keys(o).length<Object.keys(i).length)return!1;for(const f in i)if(!(f in o)||!c(o[f],i[f],r))return!1;return!0}if(e instanceof t.z.ZodArray&&n instanceof t.z.ZodArray)return c(e.element,n.element,r);if(e instanceof t.z.ZodTuple&&n instanceof t.z.ZodTuple){if(e.items.length<n.items.length)return!1;for(let o=0;o<n.items.length;o++)if(!c(e.items[o],n.items[o],r))return!1;return n._def.rest?e._def.rest?c(e._def.rest,n._def.rest,r):!1:!0}throw new Error("Failed to compare types!"+e._def.typeName+" "+n._def.typeName)};exports.isCompatibleType=c;exports.isSameType=s;
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 ignoreIntersectionsOrder: false;\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 ignoreIntersectionsOrder: false,\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","import type { EnumLike, ZodType } from \"zod\";\nimport { z } from \"zod\";\nimport {\n type IsSameTypeOptions,\n DEFAULT_COMPARE_TYPE_OPTIONS,\n isPrimitiveType,\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 );\n }\n\n // ZodUnion aka or\n if (a instanceof z.ZodUnion && b instanceof z.ZodUnion) {\n if (a.options.length !== b.options.length) return false;\n for (let i = 0; i < a.options.length; i++) {\n if (!isSameType(a.options[i], b.options[i], opts)) return false;\n }\n return true;\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 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 for (let i = 0; i < higherType.options.length; i++) {\n const match = lowerType.options.some((option: ZodType) =>\n isCompatibleType(option, lowerType.options[i], opts),\n );\n if (!match) return false;\n }\n return true;\n }\n if (higherType instanceof z.ZodUnion) {\n return higherType.options.every((option: ZodType) =>\n isCompatibleType(option, lowerType, opts),\n );\n }\n if (lowerType instanceof z.ZodUnion) {\n return lowerType.options.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","isSameType","b","options","opts","interceptorResult","aShape","bShape","key","i","optionsA","optionsB","enumA","enumB","isCompatibleType","higherType","lowerType","option","superTypeShape","subTypeShape"],"mappings":"uGAkCaA,EAA+B,CAC1C,KAAM,GACN,eAAgB,GAChB,eAAgB,GAChB,eAAgB,GAChB,cAAe,GACf,kBAAmB,GACnB,yBAA0B,GAC1B,YAAa,IAAM,CAAC,CACtB,EAGaC,EAAmBC,GAE5BA,aAAaC,EAAE,EAAA,WACfD,aAAaC,EAAAA,EAAE,WACfD,aAAaC,IAAE,QACfD,aAAaC,IAAE,WACfD,aAAaC,EAAAA,EAAE,YACfD,aAAaC,IAAE,SACfD,aAAaC,EAAA,EAAE,WACfD,aAAaC,EAAAA,EAAE,cACfD,aAAaC,IAAE,SACfD,aAAaC,EAAE,EAAA,QACfD,aAAaC,EAAAA,EAAE,YACfD,aAAaC,EAAE,EAAA,UACfD,aAAaC,EAAE,EAAA,QChCNC,EAAa,CACxBF,EACAG,EACAC,IACY,CACZ,MAAMC,EAAO,CAAE,GAAGP,EAA8B,GAAGM,CAAQ,EACrDE,EAAoBD,EAAK,YAAYL,EAAGG,EAAGE,CAAI,EACjD,GAAAC,IAAsB,IAAQA,IAAsB,GAC/C,OAAAA,EAGL,GAAAN,IAAM,QAAaG,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AAEX,KAAOU,IACT,CAACJ,EAAiBI,EAAeV,CAAG,EAAGW,EAAaX,CAAG,EAAGJ,CAAI,EACzD,MAAA,GAGJ,MAAA,EACT,CAGA,GAAIW,aAAsBf,EAAAA,EAAE,UAAYgB,aAAqBhB,EAAAA,EAAE,SAC7D,OAAOc,EAAiBC,EAAW,QAASC,EAAU,QAASZ,CAAI,EAIrE,GAAIW,aAAsBf,EAAAA,EAAE,UAAYgB,aAAqBhB,EAAAA,EAAE,SAAU,CACvE,GAAIe,EAAW,MAAM,OAASC,EAAU,MAAM,OAAe,MAAA,GAC7D,QAASP,EAAI,EAAGA,EAAIO,EAAU,MAAM,OAAQP,IACtC,GAAA,CAACK,EAAiBC,EAAW,MAAMN,CAAC,EAAGO,EAAU,MAAMP,CAAC,EAAGL,CAAI,EAC1D,MAAA,GAIP,OAAAY,EAAU,KAAK,KACZD,EAAW,KAAK,KACdD,EAAiBC,EAAW,KAAK,KAAMC,EAAU,KAAK,KAAMZ,CAAI,EADrC,GAG7B,EACT,CAEA,MAAM,IAAI,MACR,2BACEW,EAAW,KAAK,SAChB,IACAC,EAAU,KAAK,QAAA,CAErB"}
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 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 for (let i = 0; i < higherType.options.length; i++) {\n const match = lowerType.options.some((option: ZodType) =>\n isCompatibleType(option, lowerType.options[i], opts),\n );\n if (!match) return false;\n }\n return true;\n }\n if (higherType instanceof z.ZodUnion) {\n return higherType.options.every((option: ZodType) =>\n isCompatibleType(option, lowerType, opts),\n );\n }\n if (lowerType instanceof z.ZodUnion) {\n return lowerType.options.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","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,EAAE,YACfD,aAAaC,IAAE,SACfD,aAAaC,EAAA,EAAE,WACfD,aAAaC,EAAAA,EAAE,cACfD,aAAaC,IAAE,SACfD,aAAaC,EAAE,EAAA,QACfD,aAAaC,EAAAA,EAAE,YACfD,aAAaC,EAAE,EAAA,UACfD,aAAaC,EAAE,EAAA,QAMNC,EAGXC,GAEOA,EAAE,QAAQ,QAASC,GACpBA,aAAaH,IAAE,SACVC,EAAgBE,CAAC,EAEnBA,CACR,EC7CUC,EAAa,CACxBL,EACAM,EACAC,IACY,CACZ,MAAMC,EAAO,CAAE,GAAGV,EAA8B,GAAGS,CAAQ,EACrDE,EAAoBD,EAAK,YAAYR,EAAGM,EAAGE,CAAI,EACjD,GAAAC,IAAsB,IAAQA,IAAsB,GAC/C,OAAAA,EAGL,GAAAT,IAAM,QAAaM,IAAM,OAC3B,MAAM,IAAI,MAAM,2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AE,WAAawB,aAAqBxB,EAAAA,EAAE,UAAW,CACzE,MAAM0B,EAAiBH,EAAW,MAC5BI,EAAeH,EAAU,MAC3B,GAAA,OAAO,KAAKE,CAAc,EAAE,OAAS,OAAO,KAAKC,CAAY,EAAE,OAC1D,MAAA,GACT,UAAWhB,KAAOgB,EAEZ,GADA,EAAEhB,KAAOe,IACT,CAACJ,EAAiBI,EAAef,CAAG,EAAGgB,EAAahB,CAAG,EAAGJ,CAAI,EACzD,MAAA,GAGJ,MAAA,EACT,CAGA,GAAIgB,aAAsBvB,EAAAA,EAAE,UAAYwB,aAAqBxB,EAAAA,EAAE,SAC7D,OAAOsB,EAAiBC,EAAW,QAASC,EAAU,QAASjB,CAAI,EAIrE,GAAIgB,aAAsBvB,EAAAA,EAAE,UAAYwB,aAAqBxB,EAAAA,EAAE,SAAU,CACvE,GAAIuB,EAAW,MAAM,OAASC,EAAU,MAAM,OAAe,MAAA,GAC7D,QAASZ,EAAI,EAAGA,EAAIY,EAAU,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"}
package/dist/index.d.ts CHANGED
@@ -60,7 +60,6 @@ export declare interface IsSameTypeOptions {
60
60
  ignoreNullable: boolean;
61
61
  ignoreReadOnly: false;
62
62
  ignoreBranded: boolean;
63
- ignoreIntersectionsOrder: false;
64
63
  /**
65
64
  * A function that provides custom logic for comparing two ZodType instances.
66
65
  *
package/dist/index.js CHANGED
@@ -1,18 +1,17 @@
1
1
  import { z as t } from "zod";
2
- const a = {
2
+ const Z = {
3
3
  deep: !0,
4
4
  ignoreOptional: !1,
5
5
  ignoreNullable: !1,
6
6
  ignoreReadOnly: !1,
7
7
  ignoreBranded: !1,
8
8
  ignoreValidations: !0,
9
- ignoreIntersectionsOrder: !1,
10
9
  interceptor: () => {
11
10
  }
12
- }, l = (e) => e instanceof t.ZodString || e instanceof t.ZodNumber || e instanceof t.ZodNaN || e instanceof t.ZodBigInt || e instanceof t.ZodBoolean || e instanceof t.ZodDate || e instanceof t.ZodSymbol || e instanceof t.ZodUndefined || e instanceof t.ZodNull || e instanceof t.ZodAny || e instanceof t.ZodUnknown || e instanceof t.ZodNever || e instanceof t.ZodVoid, f = (e, n, d) => {
13
- const r = { ...a, ...d }, u = r.interceptor(e, n, r);
14
- if (u === !0 || u === !1)
15
- return u;
11
+ }, p = (e) => e instanceof t.ZodString || e instanceof t.ZodNumber || e instanceof t.ZodNaN || e instanceof t.ZodBigInt || e instanceof t.ZodBoolean || e instanceof t.ZodDate || e instanceof t.ZodSymbol || e instanceof t.ZodUndefined || e instanceof t.ZodNull || e instanceof t.ZodAny || e instanceof t.ZodUnknown || e instanceof t.ZodNever || e instanceof t.ZodVoid, a = (e) => e.options.flatMap((n) => n instanceof t.ZodUnion ? a(n) : n), s = (e, n, u) => {
12
+ const r = { ...Z, ...u }, d = r.interceptor(e, n, r);
13
+ if (d === !0 || d === !1)
14
+ return d;
16
15
  if (e === void 0 || n === void 0)
17
16
  throw new Error("Failed to compare type! " + e + " " + n);
18
17
  if (e === n)
@@ -24,81 +23,87 @@ const a = {
24
23
  if (e._def.typeName !== n._def.typeName)
25
24
  return !1;
26
25
  if (r.ignoreBranded)
27
- return e instanceof t.ZodBranded && (e = e.unwrap()), n instanceof t.ZodBranded && (n = n.unwrap()), f(e, n, r);
26
+ return e instanceof t.ZodBranded && (e = e.unwrap()), n instanceof t.ZodBranded && (n = n.unwrap()), s(e, n, r);
28
27
  if (e instanceof t.ZodBranded || n instanceof t.ZodBranded)
29
28
  return !1;
30
29
  if ("unwrap" in e && typeof e.unwrap == "function")
31
- return "unwrap" in n && typeof n.unwrap == "function" ? f(e.unwrap(), n.unwrap(), r) : !1;
30
+ return "unwrap" in n && typeof n.unwrap == "function" ? s(e.unwrap(), n.unwrap(), r) : !1;
32
31
  if (!r.ignoreOptional && e.isOptional() !== n.isOptional() || !r.ignoreNullable && e.isNullable() !== n.isNullable())
33
32
  return !1;
34
- if (l(e))
33
+ if (p(e))
35
34
  return !0;
36
35
  if (e instanceof t.ZodObject && n instanceof t.ZodObject) {
37
- const o = e.shape, s = n.shape;
38
- if (Object.keys(o).length !== Object.keys(s).length)
36
+ const o = e.shape, f = n.shape;
37
+ if (Object.keys(o).length !== Object.keys(f).length)
39
38
  return !1;
40
39
  for (const i in o)
41
- if (!(i in s) || !f(o[i], s[i], r))
40
+ if (!(i in f) || !s(o[i], f[i], r))
42
41
  return !1;
43
42
  return !0;
44
43
  }
45
44
  if (e instanceof t.ZodArray && n instanceof t.ZodArray)
46
- return f(e.element, n.element, r);
45
+ return s(e.element, n.element, r);
47
46
  if (e instanceof t.ZodTuple && n instanceof t.ZodTuple) {
48
47
  if (e.items.length !== n.items.length)
49
48
  return !1;
50
49
  for (let o = 0; o < e.items.length; o++)
51
- if (!f(e.items[o], n.items[o], r))
50
+ if (!s(e.items[o], n.items[o], r))
52
51
  return !1;
53
- return e._def.rest || n._def.rest ? !e._def.rest || !n._def.rest ? !1 : f(e._def.rest, n._def.rest, r) : !0;
52
+ return e._def.rest || n._def.rest ? !e._def.rest || !n._def.rest ? !1 : s(e._def.rest, n._def.rest, r) : !0;
54
53
  }
55
54
  if (e instanceof t.ZodLiteral && n instanceof t.ZodLiteral)
56
55
  return e.value === n.value;
57
56
  if (e instanceof t.ZodIntersection && n instanceof t.ZodIntersection)
58
- return f(e._def.left, n._def.left, r) && f(e._def.right, n._def.right, r);
57
+ return s(e._def.left, n._def.left, r) && s(e._def.right, n._def.right, r) || s(e._def.left, n._def.right, r) && s(e._def.right, n._def.left, r);
59
58
  if (e instanceof t.ZodUnion && n instanceof t.ZodUnion) {
60
- if (e.options.length !== n.options.length)
59
+ const o = a(e), f = a(n);
60
+ if (o.length !== f.length)
61
61
  return !1;
62
- for (let o = 0; o < e.options.length; o++)
63
- if (!f(e.options[o], n.options[o], r))
62
+ for (let i of o) {
63
+ let l = f.findIndex(
64
+ (m) => s(i, m, r)
65
+ );
66
+ if (l === -1)
64
67
  return !1;
65
- return !0;
68
+ f.splice(l, 1);
69
+ }
70
+ return f.length === 0;
66
71
  }
67
72
  if (e instanceof t.ZodReadonly && n instanceof t.ZodReadonly)
68
- return f(e._def.innerType, n._def.innerType, r);
73
+ return s(e._def.innerType, n._def.innerType, r);
69
74
  if (e instanceof t.ZodRecord && n instanceof t.ZodRecord || e instanceof t.ZodMap && n instanceof t.ZodMap)
70
- return f(e.keySchema, n.keySchema, r) && f(e.valueSchema, n.valueSchema, r);
75
+ return s(e.keySchema, n.keySchema, r) && s(e.valueSchema, n.valueSchema, r);
71
76
  if (e instanceof t.ZodSet && n instanceof t.ZodSet)
72
- return f(e._def.valueType, n._def.valueType, r);
77
+ return s(e._def.valueType, n._def.valueType, r);
73
78
  if (e instanceof t.ZodFunction && n instanceof t.ZodFunction)
74
- return f(e.parameters(), n.parameters(), r) && f(e.returnType(), n.returnType(), r);
79
+ return s(e.parameters(), n.parameters(), r) && s(e.returnType(), n.returnType(), r);
75
80
  if (e instanceof t.ZodEnum && n instanceof t.ZodEnum) {
76
- const o = e.options, s = n.options;
77
- if (o.length !== s.length)
81
+ const o = e.options, f = n.options;
82
+ if (o.length !== f.length)
78
83
  return !1;
79
84
  for (let i = 0; i < o.length; i++)
80
- if (o[i] !== s[i])
85
+ if (o[i] !== f[i])
81
86
  return !1;
82
87
  return !0;
83
88
  }
84
89
  if (e instanceof t.ZodNativeEnum && n instanceof t.ZodNativeEnum) {
85
- const o = e.enum, s = n.enum;
86
- if (Object.keys(o).length !== Object.keys(s).length)
90
+ const o = e.enum, f = n.enum;
91
+ if (Object.keys(o).length !== Object.keys(f).length)
87
92
  return !1;
88
93
  for (const i in o)
89
- if (o[i] !== s[i])
94
+ if (o[i] !== f[i])
90
95
  return !1;
91
96
  return !0;
92
97
  }
93
98
  throw console.error('Failed to compare type! "' + e, n), new Error("Unknown type! " + e._def.typeName + " " + n._def.typeName);
94
- }, c = (e, n, d) => {
99
+ }, c = (e, n, u) => {
95
100
  const r = {
96
- ...a,
97
- ...d
98
- }, u = r.interceptor(e, n, r);
99
- if (u === !0 || u === !1)
100
- return u;
101
- if (f(e, n, r))
101
+ ...Z,
102
+ ...u
103
+ }, d = r.interceptor(e, n, r);
104
+ if (d === !0 || d === !1)
105
+ return d;
106
+ if (s(e, n, r))
102
107
  return !0;
103
108
  if (!("typeName" in e._def) || !("typeName" in n._def))
104
109
  throw new Error(
@@ -125,11 +130,11 @@ const a = {
125
130
  if (e.constructor !== n.constructor)
126
131
  return !1;
127
132
  if (e instanceof t.ZodObject && n instanceof t.ZodObject) {
128
- const o = e.shape, s = n.shape;
129
- if (Object.keys(o).length < Object.keys(s).length)
133
+ const o = e.shape, f = n.shape;
134
+ if (Object.keys(o).length < Object.keys(f).length)
130
135
  return !1;
131
- for (const i in s)
132
- if (!(i in o) || !c(o[i], s[i], r))
136
+ for (const i in f)
137
+ if (!(i in o) || !c(o[i], f[i], r))
133
138
  return !1;
134
139
  return !0;
135
140
  }
@@ -149,6 +154,6 @@ const a = {
149
154
  };
150
155
  export {
151
156
  c as isCompatibleType,
152
- f as isSameType
157
+ s as isSameType
153
158
  };
154
159
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","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 ignoreIntersectionsOrder: false;\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 ignoreIntersectionsOrder: false,\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","import type { EnumLike, ZodType } from \"zod\";\nimport { z } from \"zod\";\nimport {\n type IsSameTypeOptions,\n DEFAULT_COMPARE_TYPE_OPTIONS,\n isPrimitiveType,\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 );\n }\n\n // ZodUnion aka or\n if (a instanceof z.ZodUnion && b instanceof z.ZodUnion) {\n if (a.options.length !== b.options.length) return false;\n for (let i = 0; i < a.options.length; i++) {\n if (!isSameType(a.options[i], b.options[i], opts)) return false;\n }\n return true;\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 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 for (let i = 0; i < higherType.options.length; i++) {\n const match = lowerType.options.some((option: ZodType) =>\n isCompatibleType(option, lowerType.options[i], opts),\n );\n if (!match) return false;\n }\n return true;\n }\n if (higherType instanceof z.ZodUnion) {\n return higherType.options.every((option: ZodType) =>\n isCompatibleType(option, lowerType, opts),\n );\n }\n if (lowerType instanceof z.ZodUnion) {\n return lowerType.options.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","isSameType","b","options","opts","interceptorResult","aShape","bShape","key","i","optionsA","optionsB","enumA","enumB","isCompatibleType","higherType","lowerType","option","superTypeShape","subTypeShape"],"mappings":";AAkCO,MAAMA,IAA+B;AAAA,EAC1C,MAAM;AAAA,EACN,gBAAgB;AAAA,EAChB,gBAAgB;AAAA,EAChB,gBAAgB;AAAA,EAChB,eAAe;AAAA,EACf,mBAAmB;AAAA,EACnB,0BAA0B;AAAA,EAC1B,aAAa,MAAM;AAAA,EAAC;AACtB,GAGaC,IAAkB,CAACC,MAE5BA,aAAaC,EAAE,aACfD,aAAaC,EAAE,aACfD,aAAaC,EAAE,UACfD,aAAaC,EAAE,aACfD,aAAaC,EAAE,cACfD,aAAaC,EAAE,WACfD,aAAaC,EAAE,aACfD,aAAaC,EAAE,gBACfD,aAAaC,EAAE,WACfD,aAAaC,EAAE,UACfD,aAAaC,EAAE,cACfD,aAAaC,EAAE,YACfD,aAAaC,EAAE,SChCNC,IAAa,CACxBF,GACAG,GACAC,MACY;AACZ,QAAMC,IAAO,EAAE,GAAGP,GAA8B,GAAGM,EAAQ,GACrDE,IAAoBD,EAAK,YAAYL,GAAGG,GAAGE,CAAI;AACjD,MAAAC,MAAsB,MAAQA,MAAsB;AAC/C,WAAAA;AAGL,MAAAN,MAAM,UAAaG,MAAM;AAC3B,UAAM,IAAI,MAAM,6BAA6BH,IAAI,MAAMG,CAAC;AAG1D,MAAIH,MAAMG;AACD,WAAA;AAIL,MAAAH,EAAE,gBAAgBG,EAAE;AAEf,WAAA;AAGT,MAAI,EAAE,cAAcH,EAAE,SAAS,EAAE,cAAcG,EAAE;AAC/C,UAAM,IAAI,MAAM,6BAA6BH,EAAE,OAAO,MAAMG,EAAE,IAAI;AAEpE,MAAIH,EAAE,KAAK,aAAaG,EAAE,KAAK;AACtB,WAAA;AAIT,MAAIE,E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CAAC,GAAGL,CAAI;AAC1D,eAAA;AAIP,WAAAY,EAAU,KAAK,OACZD,EAAW,KAAK,OACdD,EAAiBC,EAAW,KAAK,MAAMC,EAAU,KAAK,MAAMZ,CAAI,IADrC,KAG7B;AAAA,EACT;AAEA,QAAM,IAAI;AAAA,IACR,6BACEW,EAAW,KAAK,WAChB,MACAC,EAAU,KAAK;AAAA,EAAA;AAErB;"}
1
+ {"version":3,"file":"index.js","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 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 for (let i = 0; i < higherType.options.length; i++) {\n const match = lowerType.options.some((option: ZodType) =>\n isCompatibleType(option, lowerType.options[i], opts),\n );\n if (!match) return false;\n }\n return true;\n }\n if (higherType instanceof z.ZodUnion) {\n return higherType.options.every((option: ZodType) =>\n isCompatibleType(option, lowerType, opts),\n );\n }\n if (lowerType instanceof z.ZodUnion) {\n return lowerType.options.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","option","superTypeShape","subTypeShape"],"mappings":";AAiCO,MAAMA,IAA+B;AAAA,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AM;AAAe,aAAA;AAC7D,aAASZ,IAAI,GAAGA,IAAIY,EAAU,MAAM,QAAQZ;AACtC,UAAA,CAACU,EAAiBC,EAAW,MAAMX,CAAC,GAAGY,EAAU,MAAMZ,CAAC,GAAGL,CAAI;AAC1D,eAAA;AAIP,WAAAiB,EAAU,KAAK,OACZD,EAAW,KAAK,OACdD,EAAiBC,EAAW,KAAK,MAAMC,EAAU,KAAK,MAAMjB,CAAI,IADrC,KAG7B;AAAA,EACT;AAEA,QAAM,IAAI;AAAA,IACR,6BACEgB,EAAW,KAAK,WAChB,MACAC,EAAU,KAAK;AAAA,EAAA;AAErB;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zod-compare",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "description": "Compare two Zod schemas recursively.",
5
5
  "keywords": [
6
6
  "zod",
@@ -135,10 +135,9 @@ describe("isSameType", () => {
135
135
  isSameType(z.number().and(z.string()), z.number().and(z.string())),
136
136
  ).toBe(true);
137
137
 
138
- // order matters
139
138
  expect(
140
139
  isSameType(z.number().and(z.string()), z.string().and(z.number())),
141
- ).toBe(false);
140
+ ).toBe(true);
142
141
  });
143
142
 
144
143
  test("should compare `or` type", () => {
@@ -149,10 +148,19 @@ describe("isSameType", () => {
149
148
  isSameType(z.number().or(z.string()), z.number().or(z.string())),
150
149
  ).toBe(true);
151
150
 
152
- // order matters
153
151
  expect(
154
152
  isSameType(z.number().or(z.string()), z.string().or(z.number())),
155
- ).toBe(false);
153
+ ).toBe(true);
154
+
155
+ // should flat union type
156
+ expect(
157
+ isSameType(
158
+ // (number | string) | boolean
159
+ z.number().or(z.string()).or(z.boolean()),
160
+ // (string | boolean) | number
161
+ z.string().or(z.boolean()).or(z.number()),
162
+ ),
163
+ ).toBe(true);
156
164
  });
157
165
 
158
166
  test("should compare literal type", () => {
@@ -4,6 +4,7 @@ import {
4
4
  type IsSameTypeOptions,
5
5
  DEFAULT_COMPARE_TYPE_OPTIONS,
6
6
  isPrimitiveType,
7
+ flatUnwrapUnion,
7
8
  } from "./utils.ts";
8
9
 
9
10
  /**
@@ -131,18 +132,28 @@ export const isSameType = (
131
132
  // ZodIntersection aka and
132
133
  if (a instanceof z.ZodIntersection && b instanceof z.ZodIntersection) {
133
134
  return (
134
- isSameType(a._def.left, b._def.left, opts) &&
135
- isSameType(a._def.right, b._def.right, opts)
135
+ (isSameType(a._def.left, b._def.left, opts) &&
136
+ isSameType(a._def.right, b._def.right, opts)) ||
137
+ (isSameType(a._def.left, b._def.right, opts) &&
138
+ isSameType(a._def.right, b._def.left, opts))
136
139
  );
137
140
  }
138
141
 
139
142
  // ZodUnion aka or
140
143
  if (a instanceof z.ZodUnion && b instanceof z.ZodUnion) {
141
- if (a.options.length !== b.options.length) return false;
142
- for (let i = 0; i < a.options.length; i++) {
143
- if (!isSameType(a.options[i], b.options[i], opts)) return false;
144
+ const aOptions = flatUnwrapUnion(a as z.ZodUnion<z.ZodUnionOptions>);
145
+ const bOptions = flatUnwrapUnion(b as z.ZodUnion<z.ZodUnionOptions>);
146
+ if (aOptions.length !== bOptions.length) return false;
147
+
148
+ for (let optionA of aOptions) {
149
+ let matchIndex = bOptions.findIndex((optionB) =>
150
+ isSameType(optionA, optionB, opts),
151
+ );
152
+ if (matchIndex === -1) return false;
153
+ bOptions.splice(matchIndex, 1);
144
154
  }
145
- return true;
155
+
156
+ return bOptions.length === 0;
146
157
  }
147
158
 
148
159
  // ZodReadonly
package/src/utils.ts CHANGED
@@ -12,7 +12,6 @@ export interface IsSameTypeOptions {
12
12
  ignoreNullable: boolean;
13
13
  ignoreReadOnly: false;
14
14
  ignoreBranded: boolean;
15
- ignoreIntersectionsOrder: false;
16
15
  /**
17
16
  * A function that provides custom logic for comparing two ZodType instances.
18
17
  *
@@ -39,7 +38,6 @@ export const DEFAULT_COMPARE_TYPE_OPTIONS = {
39
38
  ignoreReadOnly: false,
40
39
  ignoreBranded: false,
41
40
  ignoreValidations: true,
42
- ignoreIntersectionsOrder: false,
43
41
  interceptor: () => {},
44
42
  } as const satisfies IsSameTypeOptions;
45
43
 
@@ -61,3 +59,18 @@ export const isPrimitiveType = (a: ZodType): boolean => {
61
59
  a instanceof z.ZodVoid
62
60
  );
63
61
  };
62
+
63
+ type Mutable<T> = { -readonly [k in keyof T]: T[k] };
64
+
65
+ export const flatUnwrapUnion = <
66
+ T extends z.ZodUnionOptions = readonly [z.ZodTypeAny, ...z.ZodTypeAny[]],
67
+ >(
68
+ t: z.ZodUnion<T>,
69
+ ): Mutable<T> => {
70
+ return t.options.flatMap((x) => {
71
+ if (x instanceof z.ZodUnion) {
72
+ return flatUnwrapUnion(x);
73
+ }
74
+ return x;
75
+ }) as unknown as T;
76
+ };