# js-joi

> 一个简易的js链式校验工具

Latest version **1.0.4** (published 2022-04-26) · ISC license · 0 weekly downloads

## Install

```sh
npm install js-joi
pnpm add js-joi
yarn add js-joi
bun add js-joi
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.4 |
| Published | 2022-04-26 |
| First published | 2019-08-30 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 9.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | iarjian |
| Maintainers | iarjian |
| Keywords | schema, verification, javascript |

## Links

- npm: https://www.npmjs.com/package/js-joi
- npm.io page: https://npm.io/package/js-joi

## Alternatives

- [vest](https://npm.io/package/vest.md) — 50.1K weekly downloads
- [@regle/core](https://npm.io/package/@regle/core.md) — 47.0K weekly downloads
- [typeof-arguments](https://npm.io/package/typeof-arguments.md) — 12.5K weekly downloads
- [@lokalise/projects-engine-contracts](https://npm.io/package/@lokalise/projects-engine-contracts.md) — 978 weekly downloads
- [@osjwnpm/nam-laboriosam-quibusdam](https://npm.io/package/@osjwnpm/nam-laboriosam-quibusdam.md) — 70 weekly downloads

## Recent versions

- 1.0.4 (latest) — 2022-04-26
- 1.0.3 — 2020-09-01
- 1.0.2 — 2019-08-30
- 1.0.1 — 2019-08-30

## README

# js-joi: 一个简易的js链式校验工具

安装

```
npm install --save js-joi
```

api说明(设置label 则label会添加到错误信息前  this._label + message)

| 事件名称     | 说明                                                         | 参数                                                     |    默认提示     |
| ------------ | :----------------------------------------------------------- | -------------------------------------------------------- | :-------------: |
| .data()      | 数据输入                                                     | (input)                                                  |       --        |
| .label()     | 标签输入                                                     | (label)                                                  |       --        |
| .required()  | 必填(不为空)                                                 | (message = '不为空')                                     |     不为空      |
| .mobile()    | 中国手机号                                                   | (message = '手机号不正确')                               |  手机号不正确   |
| .email()     | 邮箱                                                         | (message = '格式不正确')                                 |   格式不正确    |
| .images()    | 匹配图片                                                     | (message = '未知图片格式')                               |  未知图片格式   |
| .video()     | 匹配视频                                                     | (message = '未知视频格式')                               |  未知视频格式   |
| .ID()        | 身份证号 二代身份证号(18位数字),<br />最后一位是校验位,可能为数字或字符X | (message = '身份证不合法')                               |  身份证不合法   |
| .integet()   | 整数                                                         | (message = '必须为整数')                                 |   必须为整数    |
| .number()    | 数字                                                         | (message = '必须数字格式')                               |  必须数字格式   |
| .letter()    | 字母                                                         | (message = '必须是英文')                                 |   必须是英文    |
| .chinese()   | 中文                                                         | (message = '必须是中文')                                 |   必须是中文    |
| .enum()      | 枚举                                                         | (arr = [], message = `值必须为${arr.join(',')}中的一个`) |                 |
| .min()       | 最小值                                                       | (num = 0, message = `最小值为${num}`)                    |    最小值为0    |
| .max()       | 最大值                                                       | (num = 100, message = `最大值为${num}`)                  |   最大值为100   |
| .minLength() | 最小长度                                                     | (length = 6, message = `长度不能小于${length}位`)        | 长度不能小于6位 |
| .maxLength() | 最大长度                                                     | (length = 6, message = `长度不能大于${length}位`)        | 长度不能大于6位 |
| .length()    | 固定长度                                                     | (length = 6, message = `长度必须为${length}位`)          |  长度必须为6位  |
| .currency()  | 数字/货币金额<br />（支持负数、千分位分隔符）                | (message = '格式不正确')                                 |   格式不正确    |
| .Pcurrency() | 数字/货币金额<br /> (只支持正数、不支持校验千分位分隔符)     | (message = '格式不正确')                                 |   格式不正确    |
| .judge()     | 自定义判断条件校验(传入条件进行判断)                         | (condition:Boolean, message = '条件不正确')              |   条件不正确    |
| .regExp()    | 自定义正则校验                                               | (reg, message = '格式不正确')                            |   格式不正确    |

使用：

```
import Joi from 'js-joi'
```

多数据同时验证

```

const validJoi = new Joi()
validJoi
    .data('1234')
    .label('id')  // 设置label 则label会添加到错误信息前
    .required('不能为空')
    .minLength(3, '不能少于3位')
    .maxLength(5, '不能多于5位')

    // .data('1234')
    // 不设置label 则错误信息完全定制 需要输入完整错误信息
    // .required('id不能为空')

    .data('1234@qq.')
    .label('邮箱')
    .required('不能为空')
    .email('格式不正确')

    .data(13100000000)
    .label('电话号码')
    .required('不能为空')
    .mobile('电话号码输入错误')

if (!validJoi.pass) {
    // 打印错误信息
    console.log(validJoi.errorMessage)
    
    // 调用弹窗组件提示错误信息
    // this.$message.error(validJoi.errorMessage)
    return
}
```

当然你只校验一个字段的话也可以这么使用：

```
const idJoi = new Joi('1234', '数据')
idJoi
    .required('不能为空')
    .min(3, '不小于3')
    .max(1024, '不大于1024')
    .email('邮箱格式不正确')
    .regexp(/012345/, '格式错误')
```

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