Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/hexindai/egg-valid
👮 Validation plugin for eggjs
https://github.com/hexindai/egg-valid
egg egg-plugin validation
Last synced: about 1 month ago
JSON representation
👮 Validation plugin for eggjs
- Host: GitHub
- URL: https://github.com/hexindai/egg-valid
- Owner: hexindai
- License: mit
- Created: 2018-03-23T07:11:28.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-07-03T05:19:41.000Z (over 6 years ago)
- Last Synced: 2024-08-01T18:30:32.966Z (4 months ago)
- Topics: egg, egg-plugin, validation
- Language: JavaScript
- Homepage:
- Size: 55.7 KB
- Stars: 12
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-egg - egg-valid - another validate plugin for egg ![](https://img.shields.io/github/stars/hexindai/egg-valid.svg?style=social&label=Star) ![](https://img.shields.io/npm/dm/egg-valid.svg?style=flat-square) (仓库 / 插件)
- awesome-egg - egg-valid - another validate plugin for egg (Plugins)
README
# egg-valid
A better Validation egg plugin based on [@killara/validation](https://github.com/killara/validation)
[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![Test coverage][codecov-image]][codecov-url]
[![David deps][david-image]][david-url]
[![Known Vulnerabilities][snyk-image]][snyk-url]
[![npm download][download-image]][download-url][npm-image]: https://img.shields.io/npm/v/egg-valid.svg
[npm-url]: https://npmjs.org/package/egg-valid
[travis-image]: https://img.shields.io/travis/hexindai/egg-valid.svg
[travis-url]: https://travis-ci.org/hexindai/egg-valid
[codecov-image]: https://img.shields.io/codecov/c/github/hexindai/egg-valid.svg
[codecov-url]: https://codecov.io/github/hexindai/egg-valid?branch=master
[david-image]: https://img.shields.io/david/hexindai/egg-valid.svg
[david-url]: https://david-dm.org/hexindai/egg-valid
[snyk-image]: https://snyk.io/test/npm/egg-valid/badge.svg
[snyk-url]: https://snyk.io/test/npm/egg-valid
[download-image]: https://img.shields.io/npm/dm/egg-valid.svg
[download-url]: https://npmjs.org/package/egg-valid## Install
```bash
$ npm i egg-valid -S
```## Usage
```js
// config
exports.valid = {
enable: true,
package: 'egg-valid',
};
// controller
class HomeController extends Controller {
async index() {
const { app, ctx } = this;
const rule = { username: 'required|alpha' };
const errors = await app.validator.validate(ctx.request.body, rule)
// ...
}
}
```## Rule
### Derived from [@killara/validation](https://github.com/killara/validation)
* accepted
* string style: `field: 'accepted'`
* object style: `field: { accepted: true }`
* alpha
* string style: `field: 'alpha:6'` or `field: 'alpha:len=6'`
* object style: `field: { alpha: { len: 6 } }`
* confirmedRule `confirmed`: the `field` need to have the same value as the value that be filled by `field_confirmation`. We can change `field_confirmation` to any names with `confirmed:"custom"`
* string style: `field: 'confirmed'` or `field: 'confirmed:"custom_field_name"'`
* object style: `field: { confirmed: "custom_field_name" }`
* date
* string style: `field: 'date'`
* object style: `field: { date: true }`
* datetime
* string style: `field: 'datetime'`
* object style: `field: { datetime: true }`
* time
* string style: `field: 'time'`
* object style: `field: { time: true }`
* string style: `field: 'email:true'`
* object style: `field: { email: true }`
* in
* `array` style: `field: [ 'basketball', 'football' ]`
* object style: `field: { in: [ 'basketball', 'football' ] }`
* money
* string style: `field: 'money'` or `field: 'money:0'` `field: 'money:2'` (default)
* object style: `field: { money: { decimal: true } }` or `field: { money: { decimal: 0 } }` or `field: { money: { decimal: 2 } }`
* numeric
* string style: `field: 'numeric:6'` or `field: 'numeric:len=6'`
* object style: `field: { numeric: { len: 6 } }`
* regexp
* string style: `field: 'regexp:"^123456$"'`
* object style: `field: { regexp: new RegExp(/abc/, 'i') }` or `field: { regexp: /^[0-9a-zA-z]{8,16}$/ }`
* required
* string style: `field: 'required'` or `field: 'required:true'`
* object style: `field: { required: true }`**[More rules](https://github.com/killara/validation)**
### Custom rules
* phone (currently support China phone number only)
* string style: `field: 'required|phone'`
* password (length: 8-18, alphanumeric and &*;+$,?#[]%)
* string style:`field: 'password'` or `field: 'password:min=8,max=18'` or `field: 'password:"^[a-z0-9!()-._@#]{8,18}$"'`
* captcha (phone auth code)
* string style:`field: 'captcha'` or `field: 'captcha:6'`
* object style: `field: { captcha: { len: 6} }`## Messages
Customize validation messages
### Via API
```js
class HomeController extends Controller {
async index() {
const { app, ctx } = this;
const rule = { username: 'required|alpha:6' };
const messages = { 'username.alpha': '该字段应该为长度为6的字母串' };
const errors = await app.validator.validate(ctx.request.body, rule, messages);
if (errors) {
// ...
} else {
// ...
}
}
}
```### Via configuration
```js
exports.valid = {
rules: {
custom: field => context => params => {
// We can get `app` and `ctx` from context
const { app, ctx, options } = context;
//... return a boolean
}
},
messages: {
custom: 'a custom message...',
},
};
```## API
### validation.addRule(name: string, ruleFunc: (field: string) => (context: object) => (params: object) => bool)
* Add custom rule
### validation.addMessage(name: string, message: string)
* Add custom message## License
[MIT](LICENSE)