egg-joi
Install
$ npm i egg-joi --save
Usage
// {app_root}/config/plugin.jsexportsjoi = enable: true package: 'egg-joi';
Configuration
// {app_root}/config/config.default.jsexportsjoi = options: {} locale: 'zh-cn': {} throw: true // throw immediately when capture exception { return error; } // error message format when throw is true { return error; } // error message format when throw is false { return result; } // fromat result;
see config/config.default.js for more detail.
Example
app/validator/sessions
'use strict'; module { const Joi = appJoi; return login: Joiobject };
app/controller/sessions
'use strict'; module { Controller * { thisctx; // this.ctx.validate(app.validator.sessions.login, this.ctx.request.body); // this.ctx.validate(app.validator.sessions.login, this.ctx.request.body, {abortEarly: false}); see [joi] https://github.com/hapijs/joi/blob/v11.0.1/API.md // let {error, value} = this.ctx.validate(app.validator.sessions.login, false); // let {error, value} = this.ctx.validate(app.validator.sessions.login, this.ctx.request.body, false); // let {error, value} = this.ctx.validate(app.validator.sessions.login, this.ctx.request.body, {abortEarly: false}, false); thisbody = 'hello'; } return SessionsController; };
Questions & Suggestions
Please open an issue here.