Skip to content

Instantly share code, notes, and snippets.

@alex-grover
Last active June 27, 2024 23:01
Show Gist options
  • Save alex-grover/717088912f36b84ba37ae1f15872cae8 to your computer and use it in GitHub Desktop.
Save alex-grover/717088912f36b84ba37ae1f15872cae8 to your computer and use it in GitHub Desktop.
Zod + Viem
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