Quantcast
Channel: CNode:Node.js专业中文社区
Viewing all articles
Browse latest Browse all 14821

Egg.js 如何优雅的校验参数?

$
0
0

在开发过程中我们很容易在控制器内写出下面的代码:

// 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 = {
  ...
}
...

我们应当如何解耦呢,或者有无合适的轮子? 我尝试过使用装饰器,将参数校验内容注入方法,让校验与方法体更近,但是仍然有些朋克风。


Viewing all articles
Browse latest Browse all 14821

Trending Articles