/zod-file

Primary LanguageTypeScript

zod-file

npm Code style: Prettier Donate

Binary and base64 validators

 

Usage

Extend zod with a custom module, like src/zod.ts in your project:

export * from 'zod'
export * from 'zod-file'

Use it in your schema:

import * as z from './zod'

const User = z.object({
  avatar: z.base64(),
})

const parsedUser = User.parse({
  // Pass a buffer or a base64 string.
  avatar: fs.readFileSync('avatar.png'),
})

// Receive a base64 string.
const base64Avatar = parsedUser.avatar

// Invalid base64 strings will throw.
User.parse({
  avatar: 'not base64',
})