# @txjs/validator

> 一个轻量级表单数据验证工具库

Latest version **1.1.9** (published 2026-04-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install @txjs/validator
pnpm add @txjs/validator
yarn add @txjs/validator
bun add @txjs/validator
```

## Health

**Score 55/100 (C)** — status: stable.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 1.1.9 |
| Published | 2026-04-01 |
| First published | 2023-04-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 3 |
| Unpacked size | 69 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | yangtianxia |
| Maintainers | txjs |
| Keywords | txjs, @txjs/validator |

## Links

- npm: https://www.npmjs.com/package/@txjs/validator
- Repository: https://github.com/yangtianxia/txjs
- Homepage: https://github.com/yangtianxia/txjs/#readme
- Issues: https://github.com/yangtianxia/txjs/issues
- npm.io page: https://npm.io/package/@txjs/validator

## Dependencies (3)

- [extend](https://npm.io/package/extend.md) ^3.0.2
- [@txjs/bool](https://npm.io/package/@txjs/bool.md) 1.0.6
- [@txjs/shared](https://npm.io/package/@txjs/shared.md) 1.0.10

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 1.1.9 (latest) — 2026-04-01
- 1.1.8 — 2025-06-05
- 1.1.7 — 2025-06-04
- 1.1.6 — 2025-06-03
- 1.1.5 — 2025-03-19
- 1.1.4 — 2025-03-12
- 1.1.3 — 2025-03-11
- 1.1.2 — 2025-03-09
- 1.1.1 — 2025-01-16
- 1.1.0 — 2025-01-16
- 1.0.1 — 2024-12-30
- 1.0.0 — 2024-12-26
- 0.1.0 — 2024-11-24
- 0.0.10 — 2024-07-10
- 0.0.9 — 2023-11-08
- … 7 more at https://npm.io/package/@txjs/validator/versions

## README

# @txjs/validator

一个轻量级表单数据验证工具库，支持多规则校验、灵活扩展，可用于前端表单数据验证。

## 使用 npm

```javascript
npm i @txjs/validator
```

## 使用 pnpm

```javascript
pnpm add @txjs/validator
```

## 使用 yarn

```javascript
yarn add @txjs/validator
```

## Validator 参数

- trigger: 全局默认触发事件
- errorPhase: 错误生成阶段
- locale: 默认语言配置
- validation： 验证器配置，使用 `createValidation` 初始化
- messages： 错误提示配置，使用 `createMessage` 初始化

## 内置验证方法

- absoluteUrl
- contains
- email
- httpUrl
- integer
- number
- landline
- max
- min
- range
- maxlength
- minlength
- rangelength
- noscript
- required
- telephone

## 使用示例

```ts
import { Validator, createValidation, createMessage } from '@txjs/validator'
import defaults from '@txjs/validator/defaults'
import zhCN from '@txjs/validator/locale/zhCN'
import enUS from '@txjs/validator/locale/enUS'

const locale = localStorage.getItem('locale') || 'zhCN'

// 自定义验证
const validator = new Validator({
  locale,
  errorPhase: 'sync',
  validation: createValidation(defaults, {
    name: {
      type: Boolean,
      validator: (value) => {
        return value.length >= 2 && value.length <= 5
      }
    }
  }),
  messages: {
    zhCN: createMessage(zhCN, {
      name: {
        default: '[0]长度必须大于等于2，且小于等于5'
      }
    }),
    enUS: createMessage(enUS, {
      name: {
        default: '[0] The length must be greater than or equal to 2 and less than or equal to 5'
      }
    })
  }
})

const rules = validator.schema({
  username: {
    label: '用户名',
    required: true
  },
  password: {
    label: '登录密码',
    required: true
  },
  telephone: {
    label: '手机号码',
    required: true,
    telephone: true
  },
  code: {
    label: '短信验证码',
    required: true,
    maxlength: 6
  }
})
```

---
_Source: https://npm.io/package/@txjs/validator · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
