百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
Z

zod_utilz

> 编程语言
开源

用于 Zod 的框架无关工具

199 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

用于 Zod 的框架无关工具

Table of contents

  • Purpose
  • Contribute
  • Yet another library
  • Installation
    • From npm (Node/Bun)
  • Getting Started
    • import
  • Utilz
    • SPR (SafeParseResult)
    • makeErrorMap
    • useTypedParsers
    • coerce
    • useURLSearchParams
    • useFormData
    • partialSafeParse
    • json
    • stringToJSON
  • TODO

Purpose

  • Simplify common tasks in Zod
  • Fill the gap of features that might be missing in Zod
  • Provide implementations for potential new features in Zod

Contribute

Always open to ideas. Positive or negative, all are welcome. Feel free to contribute an issue or PR.

Yet another library

You might not want to install yet another library only to get access to that one Util you need. No worries. Feel free to copy and paste the code you need into your project. It won't get updated when this library gets updated, but it will reduce your bundle size. :D

Perhaps in the future there will be a way to install only the Utilz you need. If you know how to do this, please let me know.

Installation

From npm (Node/Bun)

bash
npm install zod_utilz
yarn add zod_utilz
pnpm add zod_utilz
bun add zod_utilz

Getting Started

import

Node/Bun

typescript
import { zu } from 'zod_utilz'

Deno

typescript
import { zu } from 'npm:zod_utilz'

Utilz

SPR

SPR stands for SafeParseResult

This enables optional chaining or nullish coalescing for z.SafeParseReturnType.

typescript
import { zu } from 'zod_utilz'
const schema = z.object( { foo: z.string() } )
const result = zu.SPR( schema.safeParse( { foo: 42 } ) )
const fooDataOrErrors = result.data?.foo ?? result.error?.format().foo?._errors

makeErrorMap

Simplifies the process of making a ZodErrorMap

…

useTypedParsers

Enables compile time type checking for zod parsers.

typescript
import { zu } from 'zod_utilz'
const schemaWithTypedParsers = zu.useTypedParsers( z.literal( 'foo' ) )

schemaWithTypedParsers.parse( 'foo' )
// no ts errors

schemaWithTypedParsers.parse( 'bar' )
//                            ^^^^^
// Argument of type '"bar"' is not assignable to parameter of type '"foo"'

coerce

Coercion that treats errors like normal zod errors. Prevents throwing errors when using safeParse.

z.bigint()

typescript
import { zu } from 'zod_utilz'
const bigintSchema = zu.coerce( z.bigint() )
bigintSchema.parse( '42' ) // 42n
bigintSchema.parse( '42n' ) // 42n
zu.SPR( bigintSchema.safeParse( 'foo' ) ).error?.issues[ 0 ].message
// 'Expected bigint, received string'

z.boolean()

…

z.number().array()

typescript
import { zu } from 'zod_utilz'
const numberArraySchema = zu.coerce( z.number().array() )

// if the value is not an array, it is coerced to an array with one coerced item
numberArraySchema.parse( 42 ) // [ 42 ]
numberArraySchema.parse( '42' ) // [ 42 ]

// if the value is an array, it coerces each item in the array
numberArraySchema.parse( [] ) // []
numberArraySchema.parse( [ '42', 42 ] ) // [ 42, 42 ]

zu.SPR( numberArraySchema.safeParse( 'foo' ) ).error?.issues[ 0 ].message
// 'Expected number, received nan'

useURLSearchParams

A way to parse URLSearchParams

…

useFormData

A way to parse FormData

typescript
import { zu } from 'zod_utilz'
const schema = zu.useFormData(
    z.object( {
        string: z.string(),
        number: z.number(),
        boolean: z.boolean(),
        file: z.instanceof( File ),
    } )
)
typescript
const formData = new FormData()
formData.append( 'string', 'foo' )
formData.append( 'number', '42' )
formData.append( 'boolean', 'false' )
formData.append( 'file', new File( [], 'filename.ext' ) )

zu.SPR( schema.safeParse( formData ) ).data,
// { string: 'foo', number: 42, boolean: false, file: File }
typescript
const formData = new FormData()
formData.append( 'string', '42' )
formData.append( 'number', 'false' )
formData.append( 'boolean', 'foo' )
formData.append( 'file', 'filename.ext' )

zu.SPR( schema.safeParse( formData ) ).error?.flatten().fieldErrors,
// {
//     string: [ 'Expected string, received number' ],
//     number: [ 'Expected number, received boolean' ],
//     boolean: [ 'Expected boolean, received string' ],
//     file: [ 'Input not instance of File' ],
// }

partialSafeParse

partialSafeParse allows you to get the valid fields even if there was an error in another field

typescript
import { zu } from 'zod_utilz'
const userSchema = z.object( { name: z.string(), age: z.number() } )
const result = zu.partialSafeParse( userSchema, { name: null, age: 42 } )
// {
//     successType: 'partial',
//     validData: { age: 42 },
//     invalidData: { name: null },
// }
result.error?.flatten().fieldErrors
// { name: [ 'Expected string, received null' ] }

json

zu.json() is a schema that validates that a JavaScript object is JSON-compatible. This includes string, number, boolean, and null, plus Arrays and Objects containing JSON-compatible types as values

typescript
import { zu } from 'zod_utilz'
const schema = zu.json()
schema.parse( false ) // false
schema.parse( 8675309 ) // 8675309
schema.parse( { a: 'deeply', nested: [ 'JSON', 'object' ] } )
// { a: 'deeply', nested: [ 'JSON', 'object' ] }

stringToJSON

zu.stringToJSON() is a schema that validates JSON encoded as a string, then returns the parsed value

typescript
import { zu } from 'zod_utilz'
const schema = zu.stringToJSON()
schema.parse( 'true' ) // true
schema.parse( 'null' ) // null
schema.parse( '["one", "two", "three"]' ) // ['one', 'two', 'three']
schema.parse( 'not a JSON string' ) // throws

TODO

Always open to ideas. Positive or negative, all are welcome. Feel free to contribute an issue or PR.

  • tests for lib/mapValues
  • Shrink Bundle Size
    • tree-shaking deps
      • lodash
  • zu.coerce
    • z.date()
    • z.object()
      • recursively coerce props
      • https://github.com/colinhacks/zod/discussions/1910
  • enum pick/omit
    • https://github.com/colinhacks/zod/discussions/1922
  • BaseType (Recursively get the base type of a Zod type)
    • zu.baseType( z.string() ) => z.string()
    • zu.baseType( z.string().optional() ) => z.string()
    • zu.baseType( z.string().optional().refine() ) => z.string()
    • zu.baseType( z.string().array().optional().refine() ) => z.string().array()
  • Make process for minifying
  • GitHub Actions
    • Auto publish to npm

Issues· 6 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

TypeScripttypescriptzod

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言