Joi use to verify the parameters of egg project
What is Joi
Joi is hapijs carrying data check module, package height common parameter checking function. Joi document
Project introduced Joi
Joi will be mounted in the app objects, app.js
const Joi = require('@hapi/joi');
const path = require('path');
class AppBootHook {
constructor(app) {
this.app = app;
const directory = path.join(app.config.baseDir, 'app/validator');
app.Joi = Joi;
app.loader.loadToApp(
directory,
'validator'
);
}
}
module.exports = AppBootHook;
New Joi verification document
Add Joi interception under base_contoller file
const { Controller } = require('egg');
class BaseController extends Controller {
constructor(request, response, app) {
super(request, response, app);
this.options = {
//允许存在不在 schema 中的字段
allowUnknown: true,
//过滤不存在 schema 中的字段
stripUnknown: true,
//可以在检测到第一个错误时立即返回,默认false(检查全部)
abortEarly: true,
//可以尝试将值转换为所需的类型(例如,将字符串转换为数字)
convert: true,
messages: {
'any.required': '{{#label}}不能为空',
'number.base': '{{#label}}参数错误',
'string.base': '{{#label}}参数错误'
}
};
}
assert(schema, params) {
const Joi = this.app.Joi;
const result = Joi.object(schema).validate(params, this.options);
if (result.error) {
throw new Error(result.error.details[0].message);
}
}
}
module.exports = BaseController;
Add middleware to intercept parameter check
/**
* 统一错误处理
* @param options {*}
* @param app
* @returns {errorHandler}
*/
module.exports = (options, app) => {
return async function errorHandler(ctx, next) {
try {
await next();
} catch (err) {
ctx.error = err;
let message = err.message;
if (err.status === 500) {
app.logger.error(err);
message = err.stack;
if (app.config.env === 'prod') {
message = '服务暂时不可用,正在努力修复中。';
}
}
ctx.status = err.status || 200;
ctx.body = {
code: 1,
message,
status: ctx.status
};
}
};
};
contorller use
To obtain a list of topics interface as an example
// 获取主题列表
this.assert(
{
forumId: this.app.Joi.number().required().label('版块ID')
},
this.ctx.query);