egg-validate-schema
Validate with JSON Schema
plugin for egg.
see ajv for more information.
Install
$ npm i egg-validate-schema --save
Usage
// {app_root}/config/plugin.jsexportsvalidateSchema = package: 'egg-validate-schema';
Config
fully support ajv
options, see document
// {app_root}/config/config.{env}.jsexportsvalidateSchema = // allErrors: true, // v5: true,;
Validate Request Body
const jsonSchema = "type": "object" "properties": "name": "type": "string" "info": "type": "object" "required": "name" "info" ; exports { // if validate fail will response 422 status code this; // pass your own data,default use `this.request.body` // this.validateBySchema(jsonSchema[, your_data]); // validate pass thisbody = thisrequestbody;};
validate fail response detail:
HTTP/11 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.