我有以下由分子框架创建的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
发布于 2018-07-06 03:38:13
如果只想接受"yyyy/mm/dd“格式的字符串日期,请使用pattern in string validator。
例如:
EventDate: {
type: "string",
pattern: /([12]\d{3}/(0[1-9]|1[0-2])/(0[1-9]|[12]\d|3[01]))/g,
optional: false
}https://stackoverflow.com/questions/51181871
复制相似问题