zod-guard
TypeScript icon, indicating that this package has built-in type declarations

2.0.0 • Public • Published

zod-guard

Guarding functions for zod types.

import { z } from 'zod'
import { zodGuard, zodGuardAsync } from 'zod-guard'

const MyType = z.string()
const isMyType = zodGuard(MyType)

if (isMyType(x)) {
  // `x` is definitely of MyType
  return x.substr(...)
}

const MyAsyncType = z.string().refine(async (val) => val.length < 20)
type MyAsyncType = z.infer<typeof MyAsyncType>
const isMyAsyncType = zodGuardAsync(MyAsyncType)

// Sadly there isn't the conecpt of asynchronous guards yet.
// The only way to successfully get around them is to asynchronously
// return a guard function.
if ((await isMyAsyncType(x))(x)) {
  return x.substr(...)
}

Readme

Keywords

Package Sidebar

Install

npm i zod-guard

Weekly Downloads

29

Version

2.0.0

License

MIT

Unpacked Size

37.4 kB

Total Files

9

Last publish

Collaborators

  • johngeorgewright