0

我有以下由分子框架创建的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

4

1 回答 1

1

如果您只想接受“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 
}
于 2018-07-05T19:38:12.573 回答