Is there a way to "uneval" Zod Schema - Convert schema to stringSchema #2969
Unanswered
timheerwagen
asked this question in
Q&A
Replies: 1 comment 4 replies
-
I don't know why you want to achieve this. As I know you can't convert from But for my use case I found a solution. <FormLabel>{convertToNormalCase(name)}<span className="inline-block pl-0.5 text-red-500">{!JSON.stringify(schema).includes('ZodOptional') && '*'} </span> </FormLabel> thank you |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello,
I have a Zod scheme, for example
const schema = z.string()
Now I want to convert the schema into a string that represents this schema so that I can save it later in a file:
const schemaString = "z.string()"
I have tried everything, e.g. packages like
lave
,devalue
,tosource
,serialize-javascript
,jsesc
,superjson
....My only working solution at the moment is to convert to json and then back to string with
zod-to-json-schema
andjson-schema-to-zod
.But I don't like that because the input is not always equal to the output, for example coerce doesn't work. As you can see, I already had some workarounds to implement. The code does not have to run in production, but only in the dev environment.
I would also appreciate an easier way to copy the schema into a new file.
Beta Was this translation helpful? Give feedback.
All reactions