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

Request: option to include description as comments #80

Open
jimlloyd opened this issue Mar 27, 2024 · 1 comment
Open

Request: option to include description as comments #80

jimlloyd opened this issue Mar 27, 2024 · 1 comment

Comments

@jimlloyd
Copy link

jimlloyd commented Mar 27, 2024

Given:

import { z } from 'zod'
import { zodToTs } from 'zod-to-ts'

// define your Zod schema
const UserSchema = z.object({
	username: z.string().describe("User's name"),
	age: z.number().describe("User's age"),
	inventory: z.object({
		name: z.string().describe("Item name"),
		itemId: z.number().describe("Item ID number"),
	}).array(),
})

// pass schema and name of type/identifier
const { node } = zodToTs(UserSchema, 'User')
const nodeString = printNode(node, { descriptions: true })

Result:

"{
  username: string  // User's name
  age: number  // User's age
  inventory: {
    name: string  // Item name
    itemId: number  // Item ID number
  }[]
}"

Obviously in this case the descriptions are purely redundant but for the use cases I have in mind the comments could add real value.

@kevbook
Copy link

kevbook commented Jul 31, 2024

Not sure if this package is updated. But I needed something today that had coerce, nullable, defaults, date and inline comments. So here it is, if someone can make it a module. https://gist.github.com/kevbook/861e0885296827bf15530e3a921e1246

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

2 participants