Safe parse a stringified object #367
Unanswered
christoph-fricke
asked this question in
Q&A
Replies: 1 comment
-
This doesn't exist natively in Zod currently. I recommend parsing the JSON externally to Zod (with your own try/catch logic) then pass the properly parsed JSON into your schema. The behavior of |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I recently discovered zod and absolutely love the library. Great work! 👍
A few days ago I found a perfect use case where I have to safe parse a string to an object and make sure that it conforms with a required schema. Naturally my first thought was to use zod for this.
Turns out that calling
schema.safeParse("Valid object as string")
(String is simplified) fails because a string is not an object.Calling
schema.safeParse(JSON.parse("Valid object as string"))
would work but destroys the idea ofsafeParse
sinceJSON.parse
throws an error for invalid JSON strings.Is there an option in zod to parse a stringified object? I could find something in the docs. I currently use the version
1.11.13
Beta Was this translation helpful? Give feedback.
All reactions