Release 1.0.0
Validate with JSON Schema plugin for egg.
JSON Schema
see ajv for more information.
$ npm i egg-validate-schema --save
// {app_root}/config/plugin.js exports.validateSchema = { package: 'egg-validate-schema', };
fully support ajv options, see document
ajv
// {app_root}/config/config.{env}.js exports.validateSchema = { // allErrors: true, // v5: true, };
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:
HTTP/1.1 422 Unprocessable Entity { "message": "Validation Failed", "errors": [ { "keyword": "required", "dataPath": "", "schemaPath": "#/required", "params": { missingProperty: 'name' }, "message": "should have required property 'name'", } ] }
Please open an issue here.
MIT
版权所有:中国计算机学会技术支持:开源发展技术委员会 京ICP备13000930号-9 京公网安备 11010802032778号
egg-validate-schema
Validate with
JSON Schemaplugin for egg.see ajv for more information.
Install
Usage
Config
fully support
ajvoptions, see documentValidate Request Body
validate fail response detail:
Questions & Suggestions
Please open an issue here.
License
MIT