Menu

5 Zod refinements you keep rewriting (and how to stop)
📰
0

5 Zod refinements you keep rewriting (and how to stop)

DEV Community·Ciprian Toma·about 1 month ago
#vxXiZLQ0
Reading 0:00
15s threshold

Every project starts the same way. You set up Zod, define your form schema, and then spend the next hour writing .refine() chains for fields you've validated a hundred times before. Email with disposable domain blocking. Password with uppercase, digits, special characters and length. Phone numbers that need to be E.164. Usernames that can't be "admin" or "root". IBAN that passes the mod-97 check. You've written all of these before. So have I. At some point I got tired of copy-pasting the same 40-line refinement block into every new project, so I extracted them into a library. Here are the 5 I was rewriting most often, what they look like in raw Zod, and what I replaced them with. 1. Email validation (with disposable domain blocking) The basic check is easy. The edge cases aren't. // raw Zod const email = z . string () . email () . refine ( val => ! val . endsWith ( ' + ' ), ' No plus aliases ' ) . refine ( val => { const domain = val . split ( ' @ ' )[ 1 ] return ! disposableDomains .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.

Read More