(type: z.ZodType)
| 134 | } |
| 135 | |
| 136 | function appendTypeDefinition(type: z.ZodType) { |
| 137 | switch (getTypeKind(type)) { |
| 138 | case "string": |
| 139 | return append("string"); |
| 140 | case "number": |
| 141 | case "int": |
| 142 | return append("number"); |
| 143 | case "boolean": |
| 144 | return append("boolean"); |
| 145 | case "date": |
| 146 | return append("Date"); |
| 147 | case "undefined": |
| 148 | return append("undefined"); |
| 149 | case "null": |
| 150 | return append("null"); |
| 151 | case "unknown": |
| 152 | return append("unknown"); |
| 153 | case "array": |
| 154 | return appendArrayType(type); |
| 155 | case "object": |
| 156 | return appendObjectType(type); |
| 157 | case "union": { // covers both z.union() and z.discriminatedUnion() — Zod v4 merged discriminated unions into the regular union type kind ("ZodDiscriminatedUnion" in v3); both have an `options` array |
| 158 | const unionDef = type._zod.def as z.core.$ZodDiscriminatedUnionDef | z.core.$ZodUnionDef; |
| 159 | return appendUnionOrIntersectionTypes(unionDef.options as readonly z.ZodType[], TypePrecedence.Union); |
| 160 | } |
| 161 | case "intersection": { |
| 162 | const intersectionDef = type._zod.def as z.core.$ZodIntersectionDef; |
| 163 | return appendUnionOrIntersectionTypes([intersectionDef.left as z.ZodType, intersectionDef.right as z.ZodType], TypePrecedence.Intersection); |
| 164 | } |
| 165 | case "tuple": |
| 166 | return appendTupleType(type); |
| 167 | case "record": |
| 168 | return appendRecordType(type); |
| 169 | case "literal": { |
| 170 | const litValues = (type._zod.def as z.core.$ZodLiteralDef<z.core.util.Literal>).values; |
| 171 | return append(litValues.map(v => v === null || typeof v === "string" || typeof v === "number" || typeof v === "boolean" ? JSON.stringify(v) : "any").join(" | ")); |
| 172 | } |
| 173 | case "enum": |
| 174 | return append(Object.values((type._zod.def as z.core.$ZodEnumDef).entries).map(value => JSON.stringify(value)).join(" | ")); |
| 175 | case "optional": |
| 176 | return appendUnionOrIntersectionTypes([(type._zod.def as z.core.$ZodOptionalDef).innerType as z.ZodType, z.undefined()], TypePrecedence.Union); |
| 177 | case "readonly": |
| 178 | return appendReadonlyType(type); |
| 179 | } |
| 180 | append("any"); |
| 181 | } |
| 182 | |
| 183 | function appendArrayType(arrayType: z.ZodType) { |
| 184 | appendType((arrayType._zod.def as z.core.$ZodArrayDef).element as z.ZodType, TypePrecedence.Object); |
no test coverage detected
searching dependent graphs…