https://github.com/eggjs/egg-validate-schema
validate by json-schema plugin for egg
https://github.com/eggjs/egg-validate-schema
Last synced: 5 months ago
JSON representation
validate by json-schema plugin for egg
- Host: GitHub
- URL: https://github.com/eggjs/egg-validate-schema
- Owner: eggjs
- License: mit
- Created: 2017-08-03T02:11:21.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2017-08-06T08:27:35.000Z (almost 9 years ago)
- Last Synced: 2025-10-07T18:43:11.357Z (9 months ago)
- Language: JavaScript
- Size: 5.86 KB
- Stars: 18
- Watchers: 10
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: History.md
- License: LICENSE
Awesome Lists containing this project
README
# egg-validate-schema
[![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-schema.svg?style=flat-square
[npm-url]: https://npmjs.org/package/egg-validate-schema
[travis-image]: https://img.shields.io/travis/eggjs/egg-validate-schema.svg?style=flat-square
[travis-url]: https://travis-ci.org/eggjs/egg-validate-schema
[codecov-image]: https://img.shields.io/codecov/c/github/eggjs/egg-validate-schema.svg?style=flat-square
[codecov-url]: https://codecov.io/github/eggjs/egg-validate-schema?branch=master
[david-image]: https://img.shields.io/david/eggjs/egg-validate-schema.svg?style=flat-square
[david-url]: https://david-dm.org/eggjs/egg-validate-schema
[snyk-image]: https://snyk.io/test/npm/egg-validate-schema/badge.svg?style=flat-square
[snyk-url]: https://snyk.io/test/npm/egg-validate-schema
[download-image]: https://img.shields.io/npm/dm/egg-validate-schema.svg?style=flat-square
[download-url]: https://npmjs.org/package/egg-validate-schema
Validate with `JSON Schema` plugin for egg.
see [ajv](http://epoberezkin.github.io/ajv/) for more information.
## Install
```bash
$ npm i egg-validate-schema --save
```
## Usage
```js
// {app_root}/config/plugin.js
exports.validateSchema = {
package: 'egg-validate-schema',
};
```
## Config
fully support `ajv` options, [see document](http://epoberezkin.github.io/ajv/#options)
```js
// {app_root}/config/config.{env}.js
exports.validateSchema = {
// allErrors: true,
// v5: true,
};
```
### Validate Request Body
```js
const jsonSchema = {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"info": {
"type": "object"
}
},
"required": [
"name",
"info"
],
};
exports.create = function* () {
// if validate fail will response 422 status code
this.validateBySchema(jsonSchema);
// pass your own data,default use `this.request.body`
// this.validateBySchema(jsonSchema[, your_data]);
// validate pass
this.body = this.request.body;
};
```
validate fail response detail:
```js
HTTP/1.1 422 Unprocessable Entity
{
"message": "Validation Failed",
"errors": [
{
"keyword": "required",
"dataPath": "",
"schemaPath": "#/required",
"params": { missingProperty: 'name' },
"message": "should have required property 'name'",
}
]
}
```
## Questions & Suggestions
Please open an issue [here](https://github.com/eggjs/egg/issues).
## License
[MIT](LICENSE)