Joi validating time field

折月煮酒 提交于 2020-07-07 04:25:52

问题


There is object with a property time (22:30:00).

const schema = Joi.object.keys({
  ...
  transactionDate: Joi.date().required(),
  transactionTime: Joi.time().required(), // ???
  ...
});

How to validate a time field using Joi?


回答1:


Try this way

const schema = Joi.object().keys({
   ...
   transactionDate: Joi.string().regex(/^([0-9]{2})\:([0-9]{2})$/)
})

Hear I have used simple regex format.

You can also use this : ^([01]\d|2[0-3]):?([0-5]\d)$

for AM and PM \b((1[0-2]|0?[1-9]):([0-5][0-9])([AaPp][Mm]))

AM PM



来源:https://stackoverflow.com/questions/53545182/joi-validating-time-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!