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

Feature Proposal: Preprocessed string types #1019

Closed
prescience-data opened this issue Mar 16, 2022 · 4 comments
Closed

Feature Proposal: Preprocessed string types #1019

prescience-data opened this issue Mar 16, 2022 · 4 comments
Labels
wontfix This will not be worked on

Comments

@prescience-data
Copy link

I use Zod heavily for parsing environment variables and in each project need to reimplement transformer definitions to convert the raw env object (either from process.env or dotenv) to their intended types.

What would alleviate a bunch of pain in this use case is having built in <number|boolean|bigint|null>String(), for example:

const isString = (value: unknown) : value is string => typeof value === "string"

const booleanString = z.preprocess(
  (value) => (isString(value) ? value === "true" : value),
  z.boolean()
)

const numberString = z.preprocess(
  (value) => (isString(value) ? parseInt(value) : value),
  z.number()
)

const bigintString = z.preprocess(
  (value) => (isString(value) ? BigInt(value) : value),
  z.bigint()
)

const nullString = z.preprocess(
  (value) => (isString(value) && value === "null" ? null : value),
  z.null()
)

The ideal implementation would be for the z import to provide these like:

import { z } from "zod"

const EnvSchema = z.object({
  FOO_NUMBER: z.numberString().default(100),
  BAR_BOOL: z.booleanString().default(false)
  // etc...
})

Happy to do an MR if this is something that might be accepted?

@FlorianWendelborn
Copy link
Contributor

FlorianWendelborn commented Mar 21, 2022

To me this looks more like something that could/should be implemented by a utility library. You could even include the schema itself in it, by using it like z.preprocess(...castToBoolean), so it’d really not be a lot of added code if this isn’t in zod itself.

@prescience-data
Copy link
Author

Maybe a better / more abstract interface could be something like

z.from(z.string()).number()

which would have the additional isomorphic benefit of things like

z.from(z.bigint()).number()

@stale
Copy link

stale bot commented May 21, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix This will not be worked on label May 21, 2022
@stale stale bot closed this as completed May 28, 2022
@ConProgramming
Copy link

For any modern googlers - the solution is coercion

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

3 participants