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

Add .toLowerCase() and .toUpperCase() to ZodString #2038

Merged
merged 4 commits into from Feb 26, 2023
Merged

Add .toLowerCase() and .toUpperCase() to ZodString #2038

merged 4 commits into from Feb 26, 2023

Conversation

levinuncu
Copy link
Contributor

This adds lowerCase() to ZodString.

z.string().lowerCase().parse('LOWERCASE') = 'lowercase'

@netlify
Copy link

netlify bot commented Feb 15, 2023

Deploy Preview for guileless-rolypoly-866f8a ready!

Name Link
🔨 Latest commit 325edea
🔍 Latest deploy log https://app.netlify.com/sites/guileless-rolypoly-866f8a/deploys/63fbec5ec4cbd20008818c0d
😎 Deploy Preview https://deploy-preview-2038--guileless-rolypoly-866f8a.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@JacobWeisenburger
Copy link
Collaborator

This already works:

console.log( z.string().parse( 'LOWERCASE' ).toLowerCase() ) // 'lowercase'

So why do we need another way of doing this?

@levinuncu
Copy link
Contributor Author

Than why is there this:

console.log(z.string().trim().parse(' trim ')) // 'trim'

If you could just do this:

console.log(z.string().parse(' trim ').trim()) // 'trim'

@JacobWeisenburger
Copy link
Collaborator

Personally, I don't know why we need the trim method, because like you said, we can just do this:

console.log(z.string().parse(' trim ').trim()) // 'trim'

But ultimately, it's not my call weather or not things like this make it in. I'm just trying to ask for your reasoning about why we need lowerCase. To me, adding lowerCase simply because we have trim is not a good enough reason. Because then where do we stop adding things like this? Seems like a lot of scope creep.

@levinuncu
Copy link
Contributor Author

Obviously you are right but trim() and lowerCase() could be used in scenarios where you have to parse the same zod schema several times.
So you dont have to call toLowerCase() everytime.

I know it's not ideal but imagine something like this:

const UserSchema = z.object({
	name: z.string().trim().lowerCase(),
	email: z.string().trim().lowerCase().email(),
)}

const User = UserSchema.parse(userData)

// or

const User = {
	name: userData.name.trim().toLowerCase(),
	email: userData.email.trim().toLowerCase(),
}

With zod you could reuse the Schema and don't have to pay attention to things like trim() or toLowerCase().

@colinhacks
Copy link
Owner

So why do we need another way of doing this?

There's value in only needing to declare this transform once, instead of per-parse.

I reluctantly added .trim() because I was resistant to modifying the input inside the parsing logic for ZodString. But ultimately it's a really clean API that's useful, so I decided to be less dogmatic about siloing all transformation logic in ZodEffect. We now have .endsWith and .startsWith too so it makes sense to add .toLowerCase() and .toUpperCase() too imo.

@colinhacks colinhacks merged commit 16beeb5 into colinhacks:master Feb 26, 2023
@colinhacks colinhacks changed the title lowercase method for ZodString Add .toLowerCase() and .toUpperCase() to ZodString Feb 26, 2023
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

Successfully merging this pull request may close these issues.

None yet

3 participants