https://github.com/seekcx/acr
优雅、易扩展的异步验证组件
https://github.com/seekcx/acr
acr validate validation
Last synced: about 1 month ago
JSON representation
优雅、易扩展的异步验证组件
- Host: GitHub
- URL: https://github.com/seekcx/acr
- Owner: seekcx
- License: mit
- Created: 2018-05-03T12:52:59.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2023-01-03T15:15:50.000Z (about 3 years ago)
- Last Synced: 2025-09-21T00:54:10.565Z (5 months ago)
- Topics: acr, validate, validation
- Language: JavaScript
- Homepage: https://seek.gitbook.io/acr/
- Size: 957 KB
- Stars: 10
- Watchers: 1
- Forks: 0
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Acr
[](https://npmjs.org/package/acr)
[](https://travis-ci.org/seekcx/acr)
[](https://codecov.io/gh/seekcx/acr)
[](https://github.com/prettier/prettier)
[](LICENSE)
优雅、易扩展的异步验证组件。
在实现上借鉴了 [joi](https://github.com/hapijs/joi) 和 [yup](https://github.com/jquense/yup) 的 api 风格,重新设计底层实现,具有非常强的扩展性。
## 特性
- 全异步验证
- 高度可扩展
- 链式操作(定义和使用)
- 国际化
- Typescript
- 参数命名
- 数据转换
- 默认值 [#2](https://github.com/seekcx/acr/issues/2)
- 逻辑验证 [#3](https://github.com/seekcx/acr/issues/3)
## 安装
npm
```sh
npm install acr
```
yarn
```sh
yarn add acr
```
## 使用
```js
// 验证多个
const { name, age, gender } = await acr.validate({
name: 'abel',
age: 18,
gender: ''
}, {
name: acr.string().equal('abel'),
age: acr.number().min(16).max(20),
gender: acr.string().in(['male', 'female']).default('male')
});
console.log(name, age, gender); // abel 18 male
// 条件验证
await acr.validate({
name: 'abel',
age: 18,
gender: 'male'
}, {
// 当 name 等于 abel 时,验证 age 是否在 16-20 之间
age: acr.when('name', 'abel', () => {
return acr.number().min(16).max(20);
}),
// 当闭包返回 true 时,验证 gender 必须等于 male
gender: acr.when(data => {
return data['name'] === 'abel';
}, () => {
return acr.string().equal('male');
})
});
// 验证失败处理
try {
await acr.validate({
name: 'abel',
}, {
name: acr.string().equal('zbel'),
});
} catch (error) {
console.log(error); // ValidationError
}
```
## 配置
传入配置并创建一个 `acr` 实例,接下来的一切都在这个实例上完成。
```js
const Acr = require('acr');
const acr = new Acr({
lang: 'zh-cn',
chains: {
string: {
transform: String
}
}
});
```
## 文档
[快速开始](https://seek.gitbook.io/acr/quick-start)
[配置说明](https://seek.gitbook.io/acr/config)
[详细文档](https://seek.gitbook.io/acr)
## 相关
[egg-acr](https://github.com/seekcx/egg-acr):基于 acr 实现的 eggjs 版本的异步验证插件。
## 成员
| Name | Website |
| -------- | ---------------------- |
| **abel** | |
## License
[MIT](LICENSE) © [seekcx](https://abel.seek.cx)