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

Upgrade zod version #5

Closed
poko-tin opened this issue Feb 17, 2023 · 2 comments
Closed

Upgrade zod version #5

poko-tin opened this issue Feb 17, 2023 · 2 comments

Comments

@poko-tin
Copy link

poko-tin commented Feb 17, 2023

Hi. Znv is a good project.

Recently, zod release a new version with support of corece, we do something like:

z.corece.number()

instead of:

export const numberSchema = z.preprocess((val) => {
  if (typeof val === 'string') return parseInt(val, 10);
  return val;
}, z.number());

numberSchema()

would appreciate if you upgrade zod version of znv.

@lostfictions
Copy link
Owner

lostfictions commented Feb 23, 2023

Hi there, it sounds like there might be a little confusion about what this module is for. I could probably document this better, especially in light of new zod versions that include coerce.

znv already performs coercions that are appropriate for use when parsing environment variables. You should generally not need to use preprocess or coerce, and there's definitely no need to use them for simple schemas like a number schema. znv's whole value proposition is that you do not need to write complex preprocessing schemas for your schemas unless you're doing something really unusual.

I'd invite you to take a look at the first few examples in the README, as well as the section entitled "What does znv actually do?".

As for Zod's newer coerce functionality, as noted in Zod's documentation, it has some surprising behaviours. For example, false, no, 123, and dogs all coerce to true when using the z.coerce.boolean() schema. This means Zod's coercions are often inappropriate for use in environment variable parsing, where all input values are strings. By contrast, znv tries to perform coercions that are slightly opinionated, well-documented, and appropriate for the problem domain. Again, I'd invite you to read the section in the README entitled "Coercion rules" to learn more.

I'll leave this issue open as a reminder to myself to add better documentation about coerce to the README. I may also add a section called something like "you might not need znv" -- there's already a callout under the Usage section about not using znv for general-purpose validation, but it might make sense to make the warning even more prominent.

Finally, I'd like to continue supporting older Zod versions for as long as possible rather than force users to upgrade. But I can probably add a special case in the code that yields a warning message when coerce is used.

Let me know if you still have any questions!

@poko-tin
Copy link
Author

Thank you! I have problem with znv seems to be forcing the project to use the old zod, think that I should research a way to use the new zod version by modify package.json instead 👍

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