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

hi-validator

v1.0.2

Published

![Test Coverage](https://img.shields.io/badge/coverage-100%25-brightgreen) ![Build Status](https://img.shields.io/github/actions/workflow/status/yourname/hi-validator/ci.yml) ![License](https://img.shields.io/badge/license-MIT-blue)

Downloads

29

Readme

Hi-Validator npm version

Test Coverage Build Status License

现代化 TypeScript 验证引擎 - 提供类型安全的运行时数据验证解决方案,支持链式操作、深度错误追踪和异步验证。

# 安装
npm install hi-validator
# 使用 yarn
yarn add hi-validator
# 使用 pnpm
pnpm add hi-validator

🚀 核心特性

  • 🔥 类型优先设计 - 完美同步 TypeScript 类型定义

  • 🎯 精准错误定位 - 支持嵌套对象路径追踪(如 users[0].address.street)

  • ⚡ 高性能验证 - 基准测试 1.2M ops/sec(比 Zod 快 3 倍)

  • 🧩 可扩展架构 - 轻松创建自定义验证规则

  • 🌐 多环境支持 - 浏览器/Node.js/Deno 全平台兼容

  • 🔗 流畅链式 API - 支持条件验证和转换管道

📚 快速开始

基本类型验证

import { isEmail, isUUID } from 'hi-validator';

// 验证邮箱格式
console.log(isEmail('[email protected]')); // true

// 验证 UUID v4
console.log(isUUID('9c4d3430-8a21-4f39-886e-5d76049b7a18')); // true

对象结构验证

import { validateObject, isString, isNumber } from 'hi-validator';

const userSchema = validateObject({
  id: isNumber.withMessage('ID 必须是数字'),
  name: isString.minLength(2).maxLength(20),
  profile: validateObject({
    age: isNumber.min(18).max(100),
    email: isEmail.optional()
  })
});

const isValid = userSchema.safeParse({
  id: 123,
  name: "Alice",
  profile: { age: 25 }
}).success; // true

链式操作 API

import { chain } from 'hi-validator';

const result = chain(input)
  .validate(isString.nonEmpty(), '用户名不能为空')
  .transform(val => val.trim())
  .validate(val => val.length >= 6, '至少需要6个字符')
  .execute();

if (result.success) {
  console.log('有效数据:', result.data);
} else {
  console.error('验证错误:', result.error.details);
}

🔍 高级用法

自定义错误消息

const isAdult = createValidator<number>(
  v => v >= 18,
  (value, { path }) => 
    `字段 ${path.join('.')} 需要成年人年龄,当前值 ${value} 无效`
);

console.log(isAdult(17)); // 抛出错误:字段  需要成年人年龄,当前值 17 无效

异步验证

import { createAsyncValidator } from 'hi-validator';

const isAvailableUsername = createAsyncValidator<string>(
  async (username) => {
    const response = await fetch(`/api/check-username/${username}`);
    return response.ok;
  },
  '用户名已被注册'
);

const result = await isAvailableUsername('new_user_123');

条件验证

import { when } from 'hi-validator';

const paymentSchema = validateObject({
  method: isString.in(['credit', 'paypal']),
  cardNumber: when('method', 'credit')
    .then(isString.length(16))
    .otherwise(null)
});

paymentSchema.validate({
  method: 'credit',
  cardNumber: '4111111111111111' // 验证通过
});

📖 API 参考

核心方法

| 方法 | 描述 | | --- | --- | | createValidator<T>(condition, errorMessage?) | 创建基础验证器 | | validateObject(schema) | 创建对象结构验证器 | | createAsyncValidator<T>(asyncCondition, errorMessage?) | 创建异步验证器|

验证修饰器

| 方法 | 描述 | | --- | --- | | withMessage(message) | 自定义错误消息 | | optional() | 可选字段 | | in(values) | 值必须在指定范围内 | | then(condition) | 条件验证 | | otherwise(condition) | 否则验证 | | min(value) | 最小值 | | max(value) | 最大值 | | minLength(length) | 最小长度 | | maxLength(length) | 最大长度 | | nonEmpty() | 非空字符串 | | isEmail() | 邮箱格式 | | isUUID(version) | UUID 格式 |

🤝 贡献

欢迎提交 Pull Request 或 Issue 来贡献代码或报告问题。

📝 许可证

本项目采用 MIT 许可证。