Skip to content

Save Zod schema programmatically to a file #3356

Answered by RobinTail
timheerwagen asked this question in Q&A
Discussion options

You must be logged in to vote

But i need:
const schema = z.object({ foo: z.coerce.string() })

if you want to write the actual code into the file, you can utilize factory from typescript.

Using AST Viewer you can get the exact code samples required for that purpose.

// const schema = z.object({ foo: z.coerce.string() })


  factory.createVariableStatement(
    undefined,
    factory.createVariableDeclarationList(
      [factory.createVariableDeclaration(
        factory.createIdentifier("schema"),
        undefined,
        undefined,
        factory.createCallExpression(
          factory.createPropertyAccessExpression(
            factory.createIdentifier("z"),
            factory.createIdentifier("object")
          

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by timheerwagen
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants