Last active
February 5, 2025 09:46
-
-
Save jacobparis/2a1915718f13d3fe87ffd01475279660 to your computer and use it in GitHub Desktop.
Custom Zod
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
+ "typescript.preferences.autoImportFileExcludePatterns": [ | |
+ "zod", | |
+ ] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This file allows creating custom zod helpers | |
import * as zod from 'zod'; | |
export * from 'zod'; | |
/** | |
* Coerces a checkbox value to a boolean | |
* | |
* @example z.checkbox() | |
*/ | |
export function checkbox() { | |
return zod.preprocess( | |
(value) => (value === 'on' ? true : value === 'off' ? false : undefined), | |
zod.boolean().optional(), | |
); | |
} | |
export function email() { | |
return zod | |
.string() | |
.email() | |
.transform((value) => value.toLowerCase()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment