【问题标题】:moleculer add validation rule on date format分子在日期格式上添加验证规则
【发布时间】:2018-12-13 09:05:50
【问题描述】:

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

【问题讨论】:

    标签: moleculer


    【解决方案1】:

    如果您只想接受“yyyy/mm/dd”格式的字符串日期,请使用patternin 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 
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-18
      • 2014-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-10
      • 2020-12-29
      相关资源
      最近更新 更多