Last active
July 12, 2024 04:15
-
-
Save jedwards1211/5656e43509716faac3f6596eac5203ab to your computer and use it in GitHub Desktop.
Zod Introspection Template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import z from 'zod' | |
/** | |
* If you do much runtime introspection on Zod schemas, | |
* you'll find yourself writing functions like this. | |
* | |
* Copy and adapt this to save time! | |
*/ | |
function zodIntrospect(schema: z.ZodTypeAny): any { | |
switch (schema._def.typeName) { | |
case z.ZodFirstPartyTypeKind.ZodString: | |
break | |
case z.ZodFirstPartyTypeKind.ZodNumber: | |
break | |
case z.ZodFirstPartyTypeKind.ZodNaN: | |
break | |
case z.ZodFirstPartyTypeKind.ZodBigInt: | |
break | |
case z.ZodFirstPartyTypeKind.ZodBoolean: | |
break | |
case z.ZodFirstPartyTypeKind.ZodDate: | |
break | |
case z.ZodFirstPartyTypeKind.ZodSymbol: | |
break | |
case z.ZodFirstPartyTypeKind.ZodUndefined: | |
break | |
case z.ZodFirstPartyTypeKind.ZodNull: | |
break | |
case z.ZodFirstPartyTypeKind.ZodAny: | |
break | |
case z.ZodFirstPartyTypeKind.ZodUnknown: | |
break | |
case z.ZodFirstPartyTypeKind.ZodNever: | |
break | |
case z.ZodFirstPartyTypeKind.ZodVoid: | |
break | |
case z.ZodFirstPartyTypeKind.ZodArray: { | |
const { element } = schema as z.ZodArray<z.ZodTypeAny> | |
// return zodIntrospect(element) | |
break | |
} | |
case z.ZodFirstPartyTypeKind.ZodObject: { | |
const { | |
shape, | |
_def: { unknownKeys, catchall }, | |
} = schema as z.ZodObject<z.ZodRawShape> | |
break | |
} | |
case z.ZodFirstPartyTypeKind.ZodUnion: { | |
const { options } = schema as z.ZodUnion<z.ZodUnionOptions> | |
break | |
} | |
case z.ZodFirstPartyTypeKind.ZodDiscriminatedUnion: { | |
const { discriminator, options } = schema as z.ZodDiscriminatedUnion< | |
string, | |
z.ZodDiscriminatedUnionOption<string>[] | |
> | |
break | |
} | |
case z.ZodFirstPartyTypeKind.ZodIntersection: { | |
const { | |
_def: { left, right }, | |
} = schema as z.ZodIntersection<z.ZodTypeAny, z.ZodTypeAny> | |
break | |
} | |
case z.ZodFirstPartyTypeKind.ZodTuple: { | |
const { | |
items, | |
_def: { rest }, | |
} = schema as z.ZodTuple< | |
[z.ZodTypeAny, ...z.ZodTypeAny[]] | [], | |
z.ZodTypeAny | null | |
> | |
break | |
} | |
case z.ZodFirstPartyTypeKind.ZodRecord: { | |
const { keySchema, valueSchema } = schema as z.ZodRecord< | |
z.KeySchema, | |
z.ZodTypeAny | |
> | |
break | |
} | |
case z.ZodFirstPartyTypeKind.ZodMap: { | |
const { keySchema, valueSchema } = schema as z.ZodMap< | |
z.ZodTypeAny, | |
z.ZodTypeAny | |
> | |
break | |
} | |
case z.ZodFirstPartyTypeKind.ZodSet: { | |
const { | |
_def: { valueType }, | |
} = schema as z.ZodSet<z.ZodTypeAny> | |
// return zodIntrospect(valueType) | |
break | |
} | |
case z.ZodFirstPartyTypeKind.ZodFunction: { | |
const { | |
_def: { args, returns }, | |
} = schema as z.ZodFunction<z.ZodTuple<any, any>, z.ZodTypeAny> | |
break | |
} | |
case z.ZodFirstPartyTypeKind.ZodLazy: { | |
const { schema: innerSchema } = schema as z.ZodLazy<z.ZodTypeAny> | |
// return zodIntrospect(innerSchema) | |
break | |
} | |
case z.ZodFirstPartyTypeKind.ZodLiteral: { | |
const { value } = schema as z.ZodLiteral<any> | |
break | |
} | |
case z.ZodFirstPartyTypeKind.ZodEnum: { | |
const { options } = schema as z.ZodEnum<[string, ...string[]]> | |
break | |
} | |
case z.ZodFirstPartyTypeKind.ZodEffects: { | |
const { | |
_def: { effect, schema: innerSchema }, | |
} = schema as z.ZodEffects<z.ZodTypeAny> | |
switch (effect.type) { | |
case 'preprocess': { | |
const { transform } = effect | |
break | |
} | |
case 'refinement': { | |
const { refinement } = effect | |
break | |
} | |
case 'transform': { | |
const { transform } = effect | |
break | |
} | |
} | |
// return zodIntrospect(innerSchema) | |
break | |
} | |
case z.ZodFirstPartyTypeKind.ZodNativeEnum: { | |
const { enum: _enum } = schema as z.ZodNativeEnum<z.EnumLike> | |
break | |
} | |
case z.ZodFirstPartyTypeKind.ZodOptional: { | |
const innerSchema = (schema as z.ZodOptional<z.ZodTypeAny>).unwrap() | |
// return zodIntrospect(innerSchema) | |
break | |
} | |
case z.ZodFirstPartyTypeKind.ZodNullable: { | |
const innerSchema = (schema as z.ZodNullable<z.ZodTypeAny>).unwrap() | |
// return zodIntrospect(innerSchema) | |
break | |
} | |
case z.ZodFirstPartyTypeKind.ZodDefault: { | |
const { | |
_def: { defaultValue, innerType }, | |
} = schema as z.ZodDefault<z.ZodTypeAny> | |
// return zodIntrospect(innerType) | |
break | |
} | |
case z.ZodFirstPartyTypeKind.ZodCatch: { | |
const { | |
_def: { catchValue, innerType }, | |
} = schema as z.ZodCatch<z.ZodTypeAny> | |
// return zodIntrospect(innerType) | |
break | |
} | |
case z.ZodFirstPartyTypeKind.ZodPromise: { | |
const { | |
_def: { type: innerType }, | |
} = schema as z.ZodPromise<z.ZodTypeAny> | |
// return zodIntrospect(innerType) | |
break | |
} | |
case z.ZodFirstPartyTypeKind.ZodBranded: { | |
const { | |
_def: { type: innerType }, | |
} = schema as z.ZodBranded<z.ZodTypeAny, string | number | symbol> | |
// return zodIntrospect(innerType) | |
break | |
} | |
case z.ZodFirstPartyTypeKind.ZodPipeline: { | |
const { | |
_def: { in: input, out: output }, | |
} = schema as z.ZodPipeline<z.ZodTypeAny, z.ZodTypeAny> | |
break | |
} | |
case z.ZodFirstPartyTypeKind.ZodReadonly: { | |
const { | |
_def: { innerType }, | |
} = schema as z.ZodReadonly<z.ZodTypeAny> | |
// return zodIntrospect(innerType) | |
break | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment