Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/eggjs/egg-validate
validate plugin for egg
https://github.com/eggjs/egg-validate
egg egg-plugin
Last synced: about 1 month ago
JSON representation
validate plugin for egg
- Host: GitHub
- URL: https://github.com/eggjs/egg-validate
- Owner: eggjs
- License: mit
- Created: 2016-07-17T06:23:58.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2023-03-23T05:54:23.000Z (over 1 year ago)
- Last Synced: 2024-10-29T20:37:13.537Z (about 1 month ago)
- Topics: egg, egg-plugin
- Language: JavaScript
- Size: 27.3 KB
- Stars: 264
- Watchers: 10
- Forks: 41
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- Changelog: History.md
- License: LICENSE
Awesome Lists containing this project
- awesome-egg - egg-validate - validate plugin for egg ![](https://img.shields.io/github/stars/eggjs/egg-validate.svg?style=social&label=Star) ![](https://img.shields.io/npm/dm/egg-validate.svg?style=flat-square) (仓库 / 插件)
- awesome-egg - egg-validate - validate plugin for egg (Plugins)
README
# egg-validate
[![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-validate.svg?style=flat-square
[npm-url]: https://npmjs.org/package/egg-validate
[travis-image]: https://img.shields.io/travis/eggjs/egg-validate.svg?style=flat-square
[travis-url]: https://travis-ci.org/eggjs/egg-validate
[codecov-image]: https://img.shields.io/codecov/c/github/eggjs/egg-validate.svg?style=flat-square
[codecov-url]: https://codecov.io/github/eggjs/egg-validate?branch=master
[david-image]: https://img.shields.io/david/eggjs/egg-validate.svg?style=flat-square
[david-url]: https://david-dm.org/eggjs/egg-validate
[snyk-image]: https://snyk.io/test/npm/egg-validate/badge.svg?style=flat-square
[snyk-url]: https://snyk.io/test/npm/egg-validate
[download-image]: https://img.shields.io/npm/dm/egg-validate.svg?style=flat-square
[download-url]: https://npmjs.org/package/egg-validateValidate plugin for egg.
See [parameter](https://github.com/node-modules/parameter) for more information such as custom rule.
## Install
```bash
$ npm i egg-validate --save
```## Usage
```js
// config/plugin.js
exports.validate = {
enable: true,
package: 'egg-validate',
};
```### Configurations
egg-validate support all parameter's configurations, check [parameter documents](https://github.com/node-modules/parameter) to get more infomations.
```js
// config/config.default.js
exports.validate = {
// convert: false,
// validateRoot: false,
};
```### Validate Request Body
```js
// app/controller/home.js
const Controller = require('egg').Controller;
class HomeController extends Controller {
async index() {
const { ctx, app } = this;
ctx.validate({ id: 'id' }); // will throw if invalid
// or
const errors = app.validator.validate({ id: 'id' }, ctx.request.body);
}
}
module.exports = HomeController;
```### Extend Rules
- app.js
```js
app.validator.addRule('jsonString', (rule, value) => {
try {
JSON.parse(value);
} catch (err) {
return 'must be json string';
}
});
```## Questions & Suggestions
Please open an issue [here](https://github.com/eggjs/egg/issues).
## License
[MIT](LICENSE)