npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

express-validate-kit

v0.2.0

Published

一个轻量级的 Express 请求验证工具包,支持多种验证库,包括 Joi、Yup、Zod 和 Valibot。

Readme

Express Validate Kit

一个轻量级的 Express 请求验证工具包,支持多种验证库,包括 Joi、Yup、Zod 和 Valibot。

特性

  • 🚀 轻量级:核心代码简洁,无冗余依赖
  • 🔄 灵活性:支持自定义验证逻辑和错误处理
  • 🧩 多库支持:内置对 Joi、Yup、Zod 和 Valibot 的支持
  • 🛡️ 类型安全:完整的 TypeScript 类型支持
  • 🔍 请求验证:支持验证 params、query 和 body

安装

npm install express-validate-kit

# 同时安装你需要的验证库
npm install joi     # 如果使用 Joi
npm install yup     # 如果使用 Yup
npm install zod     # 如果使用 Zod
npm install valibot # 如果使用 Valibot

基本用法

使用核心 API

如果你想使用自定义验证逻辑,可以直接使用核心 API:

import express from 'express'
import { createValidator } from 'express-validate-kit'

const app = express()

app.use(express.json())
app.use(express.urlencoded({ extended: true }))

// 创建自定义验证器
const validator = createValidator(
  {
    query: {
      page: val => {
        const nVal = parseInt(val)
        if (isNaN(nVal)) throw new Error('page is invalid')
        if (nVal < 1) throw new Error('page can not be less than 1')
        return nVal
      },
      size: val => {
        const nVal = parseInt(val)
        if (isNaN(nVal)) throw new Error('size is invalid')
        if (nVal < 1) throw new Error('size can not be less than 1')
        if (nVal > 20) throw new Error('size can not be greater than 20')
        return nVal
      },
      name: val => {
        return val?.trim() ?? ''
      }
    }
  },
  (schema, value) => {
    const data = {}
    const error = []

    for (const [key, validate] of Object.entries(schema)) {
      try {
        data[key] = validate(value[key])
      } catch (err) {
        error.push(err.message)
      }
    }

    return {
      data: error.length > 0 ? null : data,
      error: error.length > 0 ? error : null
    }
  }
)

app.get('/users', validator, (req, res) => {
  res.send({
    query: req.query
  })
})

使用 Joi

import express from 'express'
import { joiValidator } from 'express-validate-kit/joi'
import joi from 'joi'

const app = express()

app.use(express.json())
app.use(express.urlencoded({ extended: true }))

app.put(
  '/user/:id',
  joiValidator(
    {
      params: joi.object({
        id: joi.number().integer().min(1)
      }),
      body: joi.object({
        name: joi.string().min(1).required()
      })
    },
    {
      validationOptions: {
        allowUnknown: true
      }
    }
  ),
  (req, res) => {
    res.send({
      body: req.body,
      params: req.params
    })
  }
)

使用 Yup

import express from 'express'
import { yupValidator } from 'express-validate-kit/yup'
import yup from 'yup'

const app = express()

app.use(express.json())
app.use(express.urlencoded({ extended: true }))

app.put(
  '/user/:id',
  yupValidator({
    params: yup.object({
      id: yup.number().integer().min(1).required()
    }),
    body: yup.object({
      name: yup.string().min(1).required()
    })
  }),
  (req, res) => {
    res.send({
      body: req.body,
      params: req.params
    })
  }
)

使用 Zod

import express from 'express'
import { zodValidator } from 'express-validate-kit/zod'
import { z } from 'zod'

const app = express()

app.use(express.json())
app.use(express.urlencoded({ extended: true }))

app.put(
  '/user/:id',
  zodValidator(
    {
      params: z.object({
        id: z.coerce.number().int().min(1)
      }),
      body: z.object({
        name: z.string({ message: 'name must be a string' }).min(1)
      })
    },
    {
      statusCode: 400
    }
  ),
  (req, res) => {
    res.send({
      body: req.body,
      params: req.params
    })
  }
)

使用 Valibot

import express from 'express'
import { valibotValidator } from 'express-validate-kit/valibot'
import * as v from 'valibot'

const app = express()

app.use(express.json())
app.use(express.urlencoded({ extended: true }))

app.put(
  '/user/:id',
  valibotValidator(
    {
      params: v.object({
        id: v.pipe(v.string(), v.transform(Number), v.number(), v.integer(), v.minValue(1))
      }),
      body: v.object({
        name: v.pipe(v.string('name must be a string'), v.minLength(1))
      })
    },
    {
      statusCode: 400
    }
  ),
  (req, res) => {
    res.send({
      body: req.body,
      params: req.params
    })
  }
)

API 参考

核心 API

createValidator(schemas, validateFn, options?)

创建一个自定义验证中间件。

  • schemas: 包含 paramsquery 和/或 body 的对象,每个属性对应一个验证模式
  • validateFn: 验证函数,接收模式和值,返回包含 dataerror 的对象
  • options: 可选配置
    • errorResponse: 自定义错误响应函数
    • statusCode: 错误状态码

Joi API

joiValidator(schemas, options?)

创建一个 Joi 验证中间件。

  • schemas: 包含 Joi 验证模式的对象
  • options: 可选配置
    • validationOptions: Joi 验证选项
    • errorResponse: 自定义错误响应函数
    • statusCode: 错误状态码

createJoiValidator(options?)

创建一个预配置的 Joi 验证器工厂函数。

Yup API

yupValidator(schemas, options?)

创建一个 Yup 验证中间件。

  • schemas: 包含 Yup 验证模式的对象
  • options: 可选配置
    • validationOptions: Yup 验证选项
    • errorResponse: 自定义错误响应函数
    • statusCode: 错误状态码

createYupValidator(options?)

创建一个预配置的 Yup 验证器工厂函数。

Zod API

zodValidator(schemas, options?)

创建一个 Zod 验证中间件。

  • schemas: 包含 Zod 验证模式的对象
  • options: 可选配置
    • errorResponse: 自定义错误响应函数
    • statusCode: 错误状态码

createZodValidator(options?)

创建一个预配置的 Zod 验证器工厂函数。

Valibot API

valibotValidator(schemas, options?)

创建一个 Valibot 验证中间件。

  • schemas: 包含 Valibot 验证模式的对象
  • options: 可选配置
    • errorResponse: 自定义错误响应函数
    • statusCode: 错误状态码

createValibotValidator(options?)

创建一个预配置的 Valibot 验证器工厂函数。