使用Joi验证日期参数

时间:2017-10-12 08:03:39

标签: javascript node.js validation express joi

我会验证通过查询字符串传递的时间戳。这是路由代码:

路由:

router.route('/:userId/:orchardId/:sTime/:eTime')
  .get(authorize(LOGGED_USER), validate(getByInterval), controller.getByInterval);

我会验证eTime参数,使其具有实际时间戳的最大值

验证架构:

getByInterval: {
    params: {
      userId: Joi.string().regex(/^[0-9]+$/, 'numbers').required(),
      orchardId: Joi.string().regex(/^[0-9]+$/, 'numbers').required(),
      sTime: Joi.date().timestamp().required(),
      eTime: Joi.date().timestamp('unix').max(moment().unix() * 1000),
    }

试验:

it('should get 200 when data are found', async () => {
      const userId = 1;
      const orchardId = 1;
      const sTime = (moment().unix() * 1000) - 3000;
      const eTime = moment().unix() * 1000;
      return request(app)
        .get(`/v1/data/${userId}/${orchardId}/${sTime}/${eTime}`)
        .set('Authorization', `Bearer ${userAccessToken}`)
        .expect(httpStatus.OK)
    });

我试过很多方面,但没有人工作......我收到了这个错误:

错误:

{"code":400,"message":"Validation error","errors":[{"field":"eTime","location":"params","messages":["\"eTime\" must be less than or equal to \"Thu Oct 12 2017 09:49:00 GMT+0200 (CEST)\""],"types":["date.max"]}]}

0 个答案:

没有答案