Joi unix 时间戳设置最大值
Joi unix timestamp set max value
我正在使用 Joi 程序包来验证时间戳字段,但是如何在其上设置 max() 值,我希望输入时间戳小于当前时间戳
var schema = Joi.object().keys({
t: Joi.date().timestamp('unix').max(moment().unix()),
})
但我得到的错误是:
child "t" fails because ["t" must be less than or equal to "Sun Jan 18
1970 07:35:17 GMT+0330 (IRST)"]
我确定 moment().unix()
returns 当前时间戳,但这里它被强制转换为字符串。
尽管能够在您的模式中指定传入值需要 unix 时间戳,但 Joi.date().max()
似乎无法正确接受 unix 时间戳。
如果您需要在架构中使用当前日期,您可以传递字符串 'now'
而不是使用日期。或者确保您以 .max()
期望的格式输入当前日期。我用毫秒尝试了这个,它似乎按预期工作。我认为 Joi 在幕后使用默认的 Date
构造函数来构造日期以比较需要毫秒的日期。
var schema = Joi.object().keys({
t: Joi.date().timestamp('unix').max(moment().unix() * 1000)
});
来自 date.max()
上的文档
Notes: 'now' can be passed in lieu of date so as to always compare relatively to the current date, allowing to explicitly ensure a date is either in the past or in the future.
似乎 max()
和 min()
函数可以解决问题,但它们仅在以毫秒为单位指定阈值时才有效。
t: Joi.date().timestamp('unix')
.max(moment().unix() * 1000)
.min(moment().subtract('42', 'weeks').unix() * 1000),
我正在使用 Joi 程序包来验证时间戳字段,但是如何在其上设置 max() 值,我希望输入时间戳小于当前时间戳
var schema = Joi.object().keys({
t: Joi.date().timestamp('unix').max(moment().unix()),
})
但我得到的错误是:
child "t" fails because ["t" must be less than or equal to "Sun Jan 18 1970 07:35:17 GMT+0330 (IRST)"]
我确定 moment().unix()
returns 当前时间戳,但这里它被强制转换为字符串。
尽管能够在您的模式中指定传入值需要 unix 时间戳,但 Joi.date().max()
似乎无法正确接受 unix 时间戳。
如果您需要在架构中使用当前日期,您可以传递字符串 'now'
而不是使用日期。或者确保您以 .max()
期望的格式输入当前日期。我用毫秒尝试了这个,它似乎按预期工作。我认为 Joi 在幕后使用默认的 Date
构造函数来构造日期以比较需要毫秒的日期。
var schema = Joi.object().keys({
t: Joi.date().timestamp('unix').max(moment().unix() * 1000)
});
来自 date.max()
上的文档Notes: 'now' can be passed in lieu of date so as to always compare relatively to the current date, allowing to explicitly ensure a date is either in the past or in the future.
似乎 max()
和 min()
函数可以解决问题,但它们仅在以毫秒为单位指定阈值时才有效。
t: Joi.date().timestamp('unix')
.max(moment().unix() * 1000)
.min(moment().subtract('42', 'weeks').unix() * 1000),