moleculer - 分子在日期格式上添加验证规则
问题描述
我有以下由分子框架创建的api,有EventDate参数,我如何指定格式并应用验证规则来检查接收日期
createEvent: {
params: {
UserId: {
type: "string",
optional: false
},
Name: {
type: "string",
optional: false
},
Description: {
type: "string",
optional: false
},
Location: {
type: "string",
optional: false
},
EventDate: {
type: "string",
optional: false
}
},
handler(ctx) {
let entity = ctx.params;
return this.broker.call("event.find", {
query: {
UserId: entity.UserId,
Name: entity.Name,
}
}).then((res) => {
if (res == null || res.length == 0) {
return this.broker.call("event.create",{
UserId:entity.UserId,
Location: entity.Location,
EventDate: entity.EventDate,
Description: entity.Description,
Name:entity.Name
}).then(doc =>{
return new Response(200, 'success', doc);
});
} else {
throw new ValidationError("you already created event with same name", -1, "you already created event with same name");
}
});
}
},
我只想接受这种日期格式 yyyy/mm/dd
解决方案
如果您只想接受“yyyy/mm/dd”格式的字符串日期,请pattern
在字符串验证器中使用。
例如:
EventDate: {
type: "string",
pattern: /([12]\d{3}/(0[1-9]|1[0-2])/(0[1-9]|[12]\d|3[01]))/g,
optional: false
}
推荐阅读
- sapui5 - 如何使用bindAggregation ui5将列添加到控制器中的表
- java - 我正在使用 Java 制作工资单。如果我的数学公式看起来正确,为什么我会得到错误的净工资?
- r - 生成一个随机数重复“n”次的向量
- c# - 使用属性中的意外字符反序列化 Json
- python - Django中有没有办法过滤选择?
- javascript - 有没有办法不使用 React 路由器重新加载整个 html 正文?
- excel - 通过 VBA 检测 Excel 文件格式类型
- php - Laravel试图在地图函数中获取非对象的属性
- php - 运行 php 文件时如何修复“404 page not found”错误?
- macos - macOS 系统扩展:使用配置文件进行授权和签名