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

How to use zod string? #53

Closed
magick93 opened this issue Jun 24, 2023 · 2 comments
Closed

How to use zod string? #53

magick93 opened this issue Jun 24, 2023 · 2 comments

Comments

@magick93
Copy link

magick93 commented Jun 24, 2023

I've reviewed other posts on the same issue, but am unable to convert / eval the string zod schema.

  • const zod_schema = eval(parseSchema(schema)) ; results in ReferenceError: z is not defined I also tried:
const import_z = 'import {  z } from "zod";'
const combined = import_z + parseSchema(schema);

This resulted in the same error.

  • const zod_schema = eval(jsonSchemaToZod(schema)) ; results in SyntaxError: Cannot use import statement outside a module
@StefanTerdell
Copy link
Owner

Add const { z } = require('zod') or - for the billionth time - just use Ajv with the JSON schema instead of converting it

@finom
Copy link

finom commented Nov 21, 2023

for the billionth time

@StefanTerdell the package name disinform everybody who's looking how to convert JSON to Zod. Programmatic example doesn't say anything about return type. What would somebody expect to get running the following code?

const schema = jsonSchemaToZod(myObject);

I would expect to return schema instead of a string. It would be fair to rename the project and update the example. Personally for me this confusion caused me to rethink completely what I was planning to do for 2 weeks and the fact that I noticed this issue just now costs me many hours of my time because I was naively relying on the part of the documentation that clearly describes programmatic use. Please, respect people who's going to use your thing.

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

3 participants