Last active
June 27, 2024 23:01
-
-
Save alex-grover/717088912f36b84ba37ae1f15872cae8 to your computer and use it in GitHub Desktop.
Zod + Viem
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
import { getAddress } from 'viem' | |
import { z } from 'zod' | |
export const address = z | |
.string() | |
.transform((val, ctx) => { | |
try { | |
return getAddress(val.toLowerCase()) | |
} catch { | |
ctx.addIssue({ | |
code: z.ZodIssueCode.custom, | |
message: 'Not a valid address', | |
}) | |
return z.NEVER | |
} | |
}) | |
.brand('Address') | |
export type Address = z.infer<typeof address> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment