zod-compare 0.2.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +37 -10
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +46 -13
- package/dist/index.js +128 -110
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/__tests__/is-same-type.test.ts +35 -2
- package/src/create-compare-fn.ts +48 -0
- package/src/index.ts +7 -3
- package/src/is-compatible-type.ts +130 -94
- package/src/is-same-type.ts +296 -36
- package/src/rules.ts +1 -300
- package/src/types.ts +1 -0
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# Zod Compare
|
|
1
|
+
# ⚖️ Zod Compare
|
|
2
2
|
|
|
3
3
|
[](https://github.com/lawvs/zod-compare/actions/workflows/build.yml)
|
|
4
4
|
[](https://www.npmjs.com/package/zod-compare)
|
|
@@ -45,18 +45,19 @@ isCompatibleType(
|
|
|
45
45
|
|
|
46
46
|
### Custom Rules
|
|
47
47
|
|
|
48
|
-
You can use `
|
|
48
|
+
You can use `createCompareFn` to create a custom comparison function.
|
|
49
49
|
|
|
50
50
|
```ts
|
|
51
51
|
import {
|
|
52
|
-
|
|
52
|
+
createCompareFn,
|
|
53
53
|
isSameTypePresetRules,
|
|
54
54
|
defineCompareRule,
|
|
55
55
|
} from "zod-compare";
|
|
56
56
|
|
|
57
57
|
const customRule = defineCompareRule(
|
|
58
|
-
"
|
|
58
|
+
"compare description",
|
|
59
59
|
(a, b, next, recheck, context) => {
|
|
60
|
+
// If the schemas are not having the same description, return false
|
|
60
61
|
if (a.description !== b.description) {
|
|
61
62
|
return false;
|
|
62
63
|
}
|
|
@@ -64,7 +65,7 @@ const customRule = defineCompareRule(
|
|
|
64
65
|
},
|
|
65
66
|
);
|
|
66
67
|
|
|
67
|
-
const
|
|
68
|
+
const strictIsSameType = createCompareFn([
|
|
68
69
|
customRule,
|
|
69
70
|
...isSameTypePresetRules,
|
|
70
71
|
]);
|
|
@@ -84,6 +85,7 @@ isSameType(
|
|
|
84
85
|
context,
|
|
85
86
|
);
|
|
86
87
|
|
|
88
|
+
// type stacks = { name: string; target: [a: ZodType, b: ZodType]; result: boolean; }[]
|
|
87
89
|
console.log(context.stacks);
|
|
88
90
|
```
|
|
89
91
|
|
|
@@ -100,15 +102,19 @@ If there is a necessity to compare these types, custom rules can be established
|
|
|
100
102
|
Compares two Zod schemas and returns `true` if they are the same.
|
|
101
103
|
|
|
102
104
|
```ts
|
|
103
|
-
|
|
105
|
+
import { isSameType } from "zod-compare";
|
|
106
|
+
|
|
107
|
+
type isSameType: (a: ZodType, b: ZodType, context?: CompareContext) => boolean;
|
|
104
108
|
```
|
|
105
109
|
|
|
106
|
-
### `
|
|
110
|
+
### `createCompareFn`
|
|
107
111
|
|
|
108
112
|
Creates a custom comparison function.
|
|
109
113
|
|
|
110
114
|
```ts
|
|
111
|
-
|
|
115
|
+
import { createCompareFn, defineCompareRule } from "zod-compare";
|
|
116
|
+
|
|
117
|
+
type defineCompareRule = (
|
|
112
118
|
name: string,
|
|
113
119
|
rule: CompareFn,
|
|
114
120
|
) => {
|
|
@@ -116,7 +122,11 @@ const defineCompareRule: (
|
|
|
116
122
|
rule: CompareFn;
|
|
117
123
|
};
|
|
118
124
|
|
|
119
|
-
|
|
125
|
+
type createCompareFn = (rules: CompareRule[]) => typeof isSameType;
|
|
126
|
+
|
|
127
|
+
// Example
|
|
128
|
+
const isSameType = createCompareFn(isSameTypePresetRules);
|
|
129
|
+
const isCompatibleType = createCompareFn(isCompatibleTypePresetRules);
|
|
120
130
|
```
|
|
121
131
|
|
|
122
132
|
### `isCompatibleType` (Experimental API)
|
|
@@ -124,7 +134,24 @@ const createIsSameTypeFn: (rules: CompareRule[]) => typeof isSameType;
|
|
|
124
134
|
Compares two Zod schemas and returns `true` if they are compatible.
|
|
125
135
|
|
|
126
136
|
```ts
|
|
127
|
-
|
|
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]);
|
|
128
155
|
```
|
|
129
156
|
|
|
130
157
|
### Types
|
package/dist/index.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const r=require("zod"),m=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,
|
|
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],d=u.compare(t,o,()=>s(f+1),(y,_)=>n(y,_,a),a);return"stacks"in a&&Array.isArray(a.stacks)&&a.stacks.push({name:u.name,target:[t,o],result:d}),d};return s(0)};return n},h=m,p=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),l=[{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)=>p(e)&&p(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()}}],Z=m(l),z=[{name:"is same type",compare:(e,n,t)=>Z(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()}}],S=m(z),O=(e,n)=>({name:e,rule:n});exports.createCompareFn=m;exports.createIsSameTypeFn=h;exports.defineCompareRule=O;exports.isCompatibleType=S;exports.isCompatibleTypePresetRules=z;exports.isSameType=Z;exports.isSameTypePresetRules=l;
|
|
2
2
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":["../src/utils.ts","../src/rules.ts","../src/is-same-type.ts","../src/is-compatible-type.ts"],"sourcesContent":["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 type { CompareFn, CompareRule } from \"./types.ts\";\nimport { flatUnwrapUnion, isPrimitiveType } from \"./utils.ts\";\n\nexport const defineCompareRule = (name: string, rule: CompareFn) => ({\n name,\n rule,\n});\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","import type { ZodType } from \"zod\";\nimport { isSameTypePresetRules } from \"./rules.ts\";\nimport { type CompareContext, type CompareRule } from \"./types.ts\";\n\nexport const createIsSameTypeFn = (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 * 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 = createIsSameTypeFn(isSameTypePresetRules);\n","import { z, type ZodType } from \"zod\";\nimport { isSameType } from \"./is-same-type.ts\";\nimport { flatUnwrapUnion } 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): boolean => {\n if (isSameType(higherType, lowerType)) {\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());\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),\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),\n );\n }\n if (lowerType instanceof z.ZodUnion) {\n const lowerOptions = flatUnwrapUnion(lowerType);\n return lowerOptions.some((option: ZodType) =>\n isCompatibleType(higherType, option),\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])) {\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);\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])) {\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);\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":["isPrimitiveType","a","z","flatUnwrapUnion","t","x","defineCompareRule","name","rule","isSameTypePresetRules","b","next","recheck","aShape","bShape","key","i","aOptions","bOptions","optionA","matchIndex","optionB","optionsA","optionsB","enumA","enumB","createIsSameTypeFn","rules","isSameTypeFn","context","prevIndex","runner","index","isSameType","isCompatibleType","higherType","lowerType","higherOptions","lowerOptions","option","superTypeShape","subTypeShape"],"mappings":"uGAGaA,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,EC7BUC,EAAoB,CAACC,EAAcC,KAAqB,CACnE,KAAAD,EACA,KAAAC,CACF,GAEaC,EAAwB,CACnC,CACE,KAAM,kBACN,QAAS,CAACR,EAAGS,EAAGC,IAAS,CACnB,GAAAV,IAAM,QAAaS,IAAM,OAC3B,MAAM,IAAI,MAAM,2BAA6BT,EAAI,IAAMS,CAAC,EAE1D,OAAOC,EAAK,CACd,CACF,EACA,CACE,KAAM,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AC1BL,EAAiBK,EAAQH,CAAS,CAAA,EAGlC,GAAAA,aAAqBlC,IAAE,SAEzB,OADqBC,EAAgBiC,CAAS,EAC1B,KAAMG,GACxBL,EAAiBC,EAAYI,CAAM,CAAA,EAKnC,GAAAJ,EAAW,cAAgBC,EAAU,YAChC,MAAA,GAIT,GAAID,aAAsBjC,EAAAA,EAAE,WAAakC,aAAqBlC,EAAAA,EAAE,UAAW,CACzE,MAAMsC,EAAiBL,EAAW,MAC5BM,EAAeL,EAAU,MAC3B,GAAA,OAAO,KAAKI,CAAc,EAAE,OAAS,OAAO,KAAKC,CAAY,EAAE,OAC1D,MAAA,GACT,UAAW1B,KAAO0B,EAEZ,GADA,EAAE1B,KAAOyB,IACT,CAACN,EAAiBM,EAAezB,CAAG,EAAG0B,EAAa1B,CAAG,CAAC,EACnD,MAAA,GAGJ,MAAA,EACT,CAGA,GAAIoB,aAAsBjC,EAAAA,EAAE,UAAYkC,aAAqBlC,EAAAA,EAAE,SAC7D,OAAOgC,EAAiBC,EAAW,QAASC,EAAU,OAAO,EAI/D,GAAID,aAAsBjC,EAAAA,EAAE,UAAYkC,aAAqBlC,EAAAA,EAAE,SAAU,CACvE,GAAIiC,EAAW,MAAM,OAASC,EAAU,MAAM,OAAe,MAAA,GAC7D,QAASpB,EAAI,EAAGA,EAAIoB,EAAU,MAAM,OAAQpB,IACtC,GAAA,CAACkB,EAAiBC,EAAW,MAAMnB,CAAC,EAAGoB,EAAU,MAAMpB,CAAC,CAAC,EACpD,MAAA,GAIP,OAAAoB,EAAU,KAAK,KACZD,EAAW,KAAK,KACdD,EAAiBC,EAAW,KAAK,KAAMC,EAAU,KAAK,IAAI,EAD/B,GAG7B,EACT,CAEA,MAAM,IAAI,MACR,2BACED,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 const compareResult = rule.compare(\n a,\n b,\n () => runner(index + 1),\n (a, b) => isSameTypeFn(a, b, context),\n context,\n );\n\n if (\"stacks\" in context && Array.isArray(context.stacks)) {\n context.stacks.push({\n name: rule.name,\n target: [a, b],\n result: compareResult,\n });\n }\n\n return compareResult;\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","compareResult","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,EAElBE,EAAgB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AelB,CAAG,EAAGmB,EAAanB,CAAG,CAAC,EAC1C,MAAA,GAGJ,MAAA,EACT,CACA,OAAOJ,EAAK,CACd,CACF,EACA,CACE,KAAM,iBACN,QAAS,CAACiB,EAAYC,EAAWlB,EAAMC,IACjCgB,aAAsBtB,EAAAA,EAAE,UAAYuB,aAAqBvB,EAAAA,EAAE,SACtDM,EAAQgB,EAAW,QAASC,EAAU,OAAO,EAE/ClB,EAAK,CAEhB,EACA,CACE,KAAM,iBACN,QAAS,CAACiB,EAAYC,EAAWlB,EAAMC,IAAY,CACjD,GAAIgB,aAAsBtB,EAAAA,EAAE,UAAYuB,aAAqBvB,EAAAA,EAAE,SAAU,CACvE,GAAIsB,EAAW,MAAM,OAASC,EAAU,MAAM,OAAe,MAAA,GAC7D,QAASb,EAAI,EAAGA,EAAIa,EAAU,MAAM,OAAQb,IACtC,GAAA,CAACJ,EAAQgB,EAAW,MAAMZ,CAAC,EAAGa,EAAU,MAAMb,CAAC,CAAC,EAC3C,MAAA,GAIP,OAAAa,EAAU,KAAK,KACZD,EAAW,KAAK,KACdhB,EAAQgB,EAAW,KAAK,KAAMC,EAAU,KAAK,IAAI,EADtB,GAG7B,EACT,CACA,OAAOlB,EAAK,CACd,CACF,CACF,EAqBawB,EAAmB1C,EAAgBkC,CAA2B,ECtJ9DS,EAAoB,CAACC,EAAcnC,KAAqB,CACnE,KAAAmC,EACA,KAAAnC,CACF"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { CompareContext as CompareContext_2 } from './types.ts';
|
|
1
2
|
import { z } from 'zod';
|
|
2
3
|
import { ZodType } from 'zod';
|
|
3
4
|
|
|
@@ -5,6 +6,7 @@ export declare type CompareContext = {
|
|
|
5
6
|
stacks?: {
|
|
6
7
|
name: string;
|
|
7
8
|
target: [a: ZodType, b: ZodType];
|
|
9
|
+
result: boolean;
|
|
8
10
|
}[];
|
|
9
11
|
} & Record<string, unknown>;
|
|
10
12
|
|
|
@@ -15,6 +17,11 @@ export declare type CompareRule = {
|
|
|
15
17
|
compare: CompareFn;
|
|
16
18
|
};
|
|
17
19
|
|
|
20
|
+
export declare const createCompareFn: (rules: CompareRule[]) => (a: ZodType, b: ZodType, context?: CompareContext) => boolean;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @deprecated Use {@link createCompareFn} instead.
|
|
24
|
+
*/
|
|
18
25
|
export declare const createIsSameTypeFn: (rules: CompareRule[]) => (a: ZodType, b: ZodType, context?: CompareContext) => boolean;
|
|
19
26
|
|
|
20
27
|
export declare const defineCompareRule: (name: string, rule: CompareFn) => {
|
|
@@ -41,7 +48,33 @@ export declare const defineCompareRule: (name: string, rule: CompareFn) => {
|
|
|
41
48
|
* // true
|
|
42
49
|
* ```
|
|
43
50
|
*/
|
|
44
|
-
export declare const isCompatibleType: (
|
|
51
|
+
export declare const isCompatibleType: (a: z.ZodType, b: z.ZodType, context?: CompareContext_2) => boolean;
|
|
52
|
+
|
|
53
|
+
export declare const isCompatibleTypePresetRules: [{
|
|
54
|
+
readonly name: "is same type";
|
|
55
|
+
readonly compare: (higherType: z.ZodType<any, z.ZodTypeDef, any>, lowerType: z.ZodType<any, z.ZodTypeDef, any>, next: () => boolean) => boolean;
|
|
56
|
+
}, {
|
|
57
|
+
readonly name: "check typeName";
|
|
58
|
+
readonly compare: (higherType: z.ZodType<any, z.ZodTypeDef, any>, lowerType: z.ZodType<any, z.ZodTypeDef, any>, next: () => boolean) => boolean;
|
|
59
|
+
}, {
|
|
60
|
+
readonly name: "check ZodOptional/ZodNullable";
|
|
61
|
+
readonly compare: (higherType: z.ZodType<any, z.ZodTypeDef, any>, lowerType: z.ZodType<any, z.ZodTypeDef, any>, next: () => boolean, recheck: (a: z.ZodType, b: z.ZodType) => boolean) => boolean;
|
|
62
|
+
}, {
|
|
63
|
+
readonly name: "check ZodUnion";
|
|
64
|
+
readonly compare: (higherType: z.ZodType<any, z.ZodTypeDef, any>, lowerType: z.ZodType<any, z.ZodTypeDef, any>, next: () => boolean, recheck: (a: z.ZodType, b: z.ZodType) => boolean) => any;
|
|
65
|
+
}, {
|
|
66
|
+
readonly name: "compare constructor";
|
|
67
|
+
readonly compare: (higherType: z.ZodType<any, z.ZodTypeDef, any>, lowerType: z.ZodType<any, z.ZodTypeDef, any>, next: () => boolean) => boolean;
|
|
68
|
+
}, {
|
|
69
|
+
readonly name: "check ZodObject";
|
|
70
|
+
readonly compare: (higherType: z.ZodType<any, z.ZodTypeDef, any>, lowerType: z.ZodType<any, z.ZodTypeDef, any>, next: () => boolean, recheck: (a: z.ZodType, b: z.ZodType) => boolean) => boolean;
|
|
71
|
+
}, {
|
|
72
|
+
readonly name: "check ZodArray";
|
|
73
|
+
readonly compare: (higherType: z.ZodType<any, z.ZodTypeDef, any>, lowerType: z.ZodType<any, z.ZodTypeDef, any>, next: () => boolean, recheck: (a: z.ZodType, b: z.ZodType) => boolean) => boolean;
|
|
74
|
+
}, {
|
|
75
|
+
readonly name: "check ZodTuple";
|
|
76
|
+
readonly compare: (higherType: z.ZodType<any, z.ZodTypeDef, any>, lowerType: z.ZodType<any, z.ZodTypeDef, any>, next: () => boolean, recheck: (a: z.ZodType, b: z.ZodType) => boolean) => boolean;
|
|
77
|
+
}];
|
|
45
78
|
|
|
46
79
|
/**
|
|
47
80
|
* isSameType is a function that checks if two ZodTypes are the same.
|
|
@@ -62,7 +95,7 @@ export declare const isCompatibleType: (higherType: ZodType, lowerType: ZodType)
|
|
|
62
95
|
* isSameType(z.string(), z.number()); // false
|
|
63
96
|
* ```
|
|
64
97
|
*/
|
|
65
|
-
export declare const isSameType: (a: ZodType, b: ZodType, context?:
|
|
98
|
+
export declare const isSameType: (a: z.ZodType, b: z.ZodType, context?: CompareContext_2) => boolean;
|
|
66
99
|
|
|
67
100
|
export declare const isSameTypePresetRules: [{
|
|
68
101
|
readonly name: "undefined check";
|
|
@@ -81,43 +114,43 @@ export declare const isSameTypePresetRules: [{
|
|
|
81
114
|
readonly compare: (a: z.ZodType<any, z.ZodTypeDef, any>, b: z.ZodType<any, z.ZodTypeDef, any>, next: () => boolean) => boolean;
|
|
82
115
|
}, {
|
|
83
116
|
readonly name: "unwrap ZodType";
|
|
84
|
-
readonly compare: (a: z.ZodType<any, z.ZodTypeDef, any>, b: z.ZodType<any, z.ZodTypeDef, any>, next: () => boolean, recheck: (a: z.ZodType
|
|
117
|
+
readonly compare: (a: z.ZodType<any, z.ZodTypeDef, any>, b: z.ZodType<any, z.ZodTypeDef, any>, next: () => boolean, recheck: (a: z.ZodType, b: z.ZodType) => boolean) => boolean;
|
|
85
118
|
}, {
|
|
86
119
|
readonly name: "is same primitive";
|
|
87
120
|
readonly compare: (a: z.ZodType<any, z.ZodTypeDef, any>, b: z.ZodType<any, z.ZodTypeDef, any>, next: () => boolean) => boolean;
|
|
88
121
|
}, {
|
|
89
122
|
readonly name: "compare ZodObject";
|
|
90
|
-
readonly compare: (a: z.ZodType<any, z.ZodTypeDef, any>, b: z.ZodType<any, z.ZodTypeDef, any>, next: () => boolean, recheck: (a: z.ZodType
|
|
123
|
+
readonly compare: (a: z.ZodType<any, z.ZodTypeDef, any>, b: z.ZodType<any, z.ZodTypeDef, any>, next: () => boolean, recheck: (a: z.ZodType, b: z.ZodType) => boolean) => boolean;
|
|
91
124
|
}, {
|
|
92
125
|
readonly name: "compare ZodArray";
|
|
93
|
-
readonly compare: (a: z.ZodType<any, z.ZodTypeDef, any>, b: z.ZodType<any, z.ZodTypeDef, any>, next: () => boolean, recheck: (a: z.ZodType
|
|
126
|
+
readonly compare: (a: z.ZodType<any, z.ZodTypeDef, any>, b: z.ZodType<any, z.ZodTypeDef, any>, next: () => boolean, recheck: (a: z.ZodType, b: z.ZodType) => boolean) => boolean;
|
|
94
127
|
}, {
|
|
95
128
|
readonly name: "compare ZodTuple";
|
|
96
|
-
readonly compare: (a: z.ZodType<any, z.ZodTypeDef, any>, b: z.ZodType<any, z.ZodTypeDef, any>, next: () => boolean, recheck: (a: z.ZodType
|
|
129
|
+
readonly compare: (a: z.ZodType<any, z.ZodTypeDef, any>, b: z.ZodType<any, z.ZodTypeDef, any>, next: () => boolean, recheck: (a: z.ZodType, b: z.ZodType) => boolean) => boolean;
|
|
97
130
|
}, {
|
|
98
131
|
readonly name: "compare ZodLiteral";
|
|
99
132
|
readonly compare: (a: z.ZodType<any, z.ZodTypeDef, any>, b: z.ZodType<any, z.ZodTypeDef, any>, next: () => boolean) => boolean;
|
|
100
133
|
}, {
|
|
101
134
|
readonly name: "compare ZodIntersection";
|
|
102
|
-
readonly compare: (a: z.ZodType<any, z.ZodTypeDef, any>, b: z.ZodType<any, z.ZodTypeDef, any>, next: () => boolean, recheck: (a: z.ZodType
|
|
135
|
+
readonly compare: (a: z.ZodType<any, z.ZodTypeDef, any>, b: z.ZodType<any, z.ZodTypeDef, any>, next: () => boolean, recheck: (a: z.ZodType, b: z.ZodType) => boolean) => boolean;
|
|
103
136
|
}, {
|
|
104
137
|
readonly name: "compare ZodUnion";
|
|
105
|
-
readonly compare: (a: z.ZodType<any, z.ZodTypeDef, any>, b: z.ZodType<any, z.ZodTypeDef, any>, next: () => boolean, recheck: (a: z.ZodType
|
|
138
|
+
readonly compare: (a: z.ZodType<any, z.ZodTypeDef, any>, b: z.ZodType<any, z.ZodTypeDef, any>, next: () => boolean, recheck: (a: z.ZodType, b: z.ZodType) => boolean) => boolean;
|
|
106
139
|
}, {
|
|
107
140
|
readonly name: "compare ZodReadonly";
|
|
108
|
-
readonly compare: (a: z.ZodType<any, z.ZodTypeDef, any>, b: z.ZodType<any, z.ZodTypeDef, any>, next: () => boolean, recheck: (a: z.ZodType
|
|
141
|
+
readonly compare: (a: z.ZodType<any, z.ZodTypeDef, any>, b: z.ZodType<any, z.ZodTypeDef, any>, next: () => boolean, recheck: (a: z.ZodType, b: z.ZodType) => boolean) => boolean;
|
|
109
142
|
}, {
|
|
110
143
|
readonly name: "compare ZodRecord";
|
|
111
|
-
readonly compare: (a: z.ZodType<any, z.ZodTypeDef, any>, b: z.ZodType<any, z.ZodTypeDef, any>, next: () => boolean, recheck: (a: z.ZodType
|
|
144
|
+
readonly compare: (a: z.ZodType<any, z.ZodTypeDef, any>, b: z.ZodType<any, z.ZodTypeDef, any>, next: () => boolean, recheck: (a: z.ZodType, b: z.ZodType) => boolean) => boolean;
|
|
112
145
|
}, {
|
|
113
146
|
readonly name: "compare ZodMap";
|
|
114
|
-
readonly compare: (a: z.ZodType<any, z.ZodTypeDef, any>, b: z.ZodType<any, z.ZodTypeDef, any>, next: () => boolean, recheck: (a: z.ZodType
|
|
147
|
+
readonly compare: (a: z.ZodType<any, z.ZodTypeDef, any>, b: z.ZodType<any, z.ZodTypeDef, any>, next: () => boolean, recheck: (a: z.ZodType, b: z.ZodType) => boolean) => boolean;
|
|
115
148
|
}, {
|
|
116
149
|
readonly name: "compare ZodSet";
|
|
117
|
-
readonly compare: (a: z.ZodType<any, z.ZodTypeDef, any>, b: z.ZodType<any, z.ZodTypeDef, any>, next: () => boolean, recheck: (a: z.ZodType
|
|
150
|
+
readonly compare: (a: z.ZodType<any, z.ZodTypeDef, any>, b: z.ZodType<any, z.ZodTypeDef, any>, next: () => boolean, recheck: (a: z.ZodType, b: z.ZodType) => boolean) => boolean;
|
|
118
151
|
}, {
|
|
119
152
|
readonly name: "compare ZodFunction";
|
|
120
|
-
readonly compare: (a: z.ZodType<any, z.ZodTypeDef, any>, b: z.ZodType<any, z.ZodTypeDef, any>, next: () => boolean, recheck: (a: z.ZodType
|
|
153
|
+
readonly compare: (a: z.ZodType<any, z.ZodTypeDef, any>, b: z.ZodType<any, z.ZodTypeDef, any>, next: () => boolean, recheck: (a: z.ZodType, b: z.ZodType) => boolean) => boolean;
|
|
121
154
|
}, {
|
|
122
155
|
readonly name: "compare ZodEnum";
|
|
123
156
|
readonly compare: (a: z.ZodType<any, z.ZodTypeDef, any>, b: z.ZodType<any, z.ZodTypeDef, any>, next: () => boolean) => boolean;
|