Egg.js 如何优雅的校验参数?
在开发过程中我们很容易在控制器内写出下面的代码:
// app/controller/topics.js
const Controller = require('egg').Controller;
// 定义创建接口的请求参数规则
const createRule = {
...
};
class TopicController extends Controller {
async create() {
...
}
}
module.exports = TopicController;
在接口较少的时候尚能条理清晰。一旦接口多起来,接口逻辑就会被行数众多的参数校验掩盖。
const createRule = {
...
}
const selectRule = {
...
}
const destroyRule = {
...
}
const updateRule = {
...
}
...
我们应当如何解耦呢,或者有无合适的轮子? 我尝试过使用装饰器,将参数校验内容注入方法,让校验与方法体更近,但是仍然有些朋克风。
19 回复
这是个好问题,我的思路是:
- 使用TS
- json序列化到类
- 使用装饰器,重写属性的get/set方法,校验在get/set中做,然后把序列化好之后的body,注入到controller中,作为函数参数。
伪代码如下
class User{
name:string;
id:number;
}
class xxx extends controller{
@checkBody(User)
async index(body:User){
.....
}
}
不知道社区有没有更好的做法。
@215566435 TS 的话应该就这样了,不一定要作为方法装饰器,也可以是参数装饰器。
JS 的话,把这些规则放到 app/validate 下,然后写个 loader 自动挂载到 app.validateRule.xx 即可