Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation for ZodCodeGenerator #335

Closed
Foo42 opened this issue Mar 18, 2021 · 5 comments
Closed

Documentation for ZodCodeGenerator #335

Foo42 opened this issue Mar 18, 2021 · 5 comments

Comments

@Foo42
Copy link

Foo42 commented Mar 18, 2021

I recently discovered the ZodCodeGenerator while browsing the source, and after discovering how to use it with some trial and error am keen to make use of it for some code generation within a project I'm on. However, I noticed there was no documentation for it and don't wish to rely on a feature which may not be considered part of the libraries stable API.

Is the lack of documentation intentional or accidental?

@colinhacks
Copy link
Owner

colinhacks commented Mar 18, 2021

I'm afraid it's very intentional, in fact all that functionality was just removed in the newest version of Zod 3. It was something I was playing around with, but I don't think it makes sense for that to be in the core library.

That said, if you (or anyone else reading this) wanted to split that out into a separate utility, I'd be happy to link to it in the readme. You can use my old implementation as a starting point (though it'll need some refactoring to work with the newest Zod 3 version, which eliminated the ZodTypes enum for the sake of easier extensibility).

@SuchMonkey
Copy link

This seems like something I would need...maybe?
Or maybe I'm just missing something else.

I am looking for a way to iterate over a Zod schema.
Either recursively or by first flattening the schema and working with paths.

To be precise:
From a given schema I want to derive (react) props for form fields, like type="text|number", required, etc...

What would be the best way to approach this?

@colinhacks
Copy link
Owner

colinhacks commented Mar 22, 2021

Zod now exports a type called ZodFirstPartySchemaTypes which is a big union of all the types defined by Zod:

export type ZodFirstPartySchemaTypes =
  | ZodString
  | ZodNumber
  | ZodBigInt
  | ZodBoolean
  | ZodDate
  | ZodUndefined
  | ZodNull
  | ZodAny
  | ZodUnknown
  | ZodNever
  | ZodVoid
  | ZodArray<any>
  | ZodObject<any>
  | ZodUnion<any>
  | ZodIntersection<any, any>
  | ZodTuple
  | ZodRecord
  | ZodMap
  | ZodSet
  | ZodFunction<any, any>
  | ZodLazy<any>
  | ZodLiteral<any>
  | ZodEnum<any>
  | ZodEffects<any>
  | ZodNativeEnum<any>
  | ZodOptional<any>
  | ZodNullable<any>
  | ZodPromise<any>;

If you have an generic instance of ZodType, just cast it to ZodFirstPartySchemaTypes and use instanceof to differentiate:

const codegen = (_schema: z.ZodTypeAny)=>{
  const schema: ZodFirstPartySchemaTypes = _schema as any;
  if (schema instanceof ZodString) {
    schema._def.isEmail;
  }else if(schema instanceof ZodNumber){
    // etc
  }
}

TypeScript is smart enough to properly type schema inside each if statement (so you can access properties that are specific to a particular subclass).

@SuchMonkey
Copy link

Thank you very much for your insights :)

@HannesP
Copy link

HannesP commented Apr 5, 2021

I'm working on an RPC library for personal use, where I'm currently relying on io-ts. I just found out about Zod's much more convenient way to work with optional parameters and would love to switch, but I'm dependent on being able to generate valid client code. Here's an example to describe the use case: https://github.com/farcry-ts/farcry#example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants