Skip to content

Instantly share code, notes, and snippets.

@jacobparis
Last active February 5, 2025 09:46
Show Gist options
  • Save jacobparis/2a1915718f13d3fe87ffd01475279660 to your computer and use it in GitHub Desktop.
Save jacobparis/2a1915718f13d3fe87ffd01475279660 to your computer and use it in GitHub Desktop.
Custom Zod
{
+ "typescript.preferences.autoImportFileExcludePatterns": [
+ "zod",
+ ]
}
// 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