https://github.com/xanke/egg-async-validator-stage
https://github.com/xanke/egg-async-validator-stage
Last synced: about 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/xanke/egg-async-validator-stage
- Owner: xanke
- Created: 2019-01-08T03:24:37.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2020-09-15T00:31:20.000Z (over 5 years ago)
- Last Synced: 2025-03-21T15:12:54.100Z (about 1 year ago)
- Language: JavaScript
- Size: 3.91 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# egg-async-validator-stage
Validate plugin for egg, compatible [egg-validate](https://github.com/eggjs/egg-validate) method.
see [async-validate](https://github.com/yiminghe/async-validator) for more information such as custom rule.
## Install
```bash
$ npm i egg-async-validator-stage --save
```
## Usage
```js
// {app_root}/config/plugin.js
exports.validate = {
enable: true,
package: 'egg-async-validator-stage',
};
```
## Configuration
```js
// {app_root}/config/config.default.js
exports.validate = {
messages: {},
};
```
see [async-validator#messages](https://github.com/yiminghe/async-validator#messages) for more detail.
## Example
```js
// {app_root}/app/controller/home.js
exports.index = async ctx => {
// will throw if invalid.
await ctx.validate({
data: { type: 'object', required: true },
}, {
data: '1',
});
// if you want to get errors info,
// use try { ... } catch (err) { ... } to get
// example
try {
await ctx.validate({ data: { type: 'object', required: true } }); // validate target, default to `this.request.body`
} catch (err) {
const errors = err.errors;
}
};
```
rules see [async-validator#rules](https://github.com/yiminghe/async-validator#rules) for more detail.
> !!important: `method`,`date`,`hex` in `type` does not apply to server - side form validation