Getting Started
Installation
Install the plugin:
bash
npm add zod@latest
npm add -D unplugin-valype
bash
yarn add zod@latest
yarn add -D unplugin-valype
bash
pnpm add zod@latest
pnpm add -D unplugin-valype
bash
bun add zod@latest
bun add -D unplugin-valype
zod's version should be
3.25.0
at least, because valype generates zod v4 schema
Configuration
Add the plugin to your build tool configuration:
Vite
ts
// vite.config.ts
import valype from 'unplugin-valype/vite'
export default defineConfig({
plugins: [
valype({ /* options */ }),
],
})
Rollup
ts
// rollup.config.js
import valype from 'unplugin-valype/rollup'
export default {
plugins: [
valype({ /* options */ }),
],
}
Webpack
ts
// webpack.config.js
module.exports = {
plugins: [
require('unplugin-valype/webpack')({ /* options */ })
]
}
Nuxt
ts
// nuxt.config.js
export default defineNuxtConfig({
modules: [
['unplugin-valype/nuxt', { /* options */ }],
],
})
Vue CLI
ts
// vue.config.js
module.exports = {
configureWebpack: {
plugins: [
require('unplugin-valype/webpack')({ /* options */ }),
],
},
}
esbuild
ts
// esbuild.config.js
import { build } from 'esbuild'
import valype from 'unplugin-valype/esbuild'
build({
plugins: [valype()],
})
Usage
- Define your types (use
.valype.ts
extension):
typescript
// user.valype.ts
export interface User {
name: string
age: number
}
- Use the generated validator:
typescript
import { validateUser } from './user.valype'
const issues = validateUser(data) // Returns ZodIssue[] or undefined
if (issues) {
// Handle validation errors
}
Validator Signature
unplugin-valype
generates validators for types exported from *.valype.ts
files:
typescript
export declare function validateSome(data: unknown): ZodIssue[] | undefined
- Returns
undefined
when validation passes - Returns
ZodIssue[]
with error details when validation fails
Note: You typically don't need to use
valype
package directly, just use the generated validators.