Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Joi unix timestamp set max value

Tags:

momentjs

joi

I'm using Joi package to validate a timestamp field but How can I set a max() value on it, I want the input timestamp to be less than current time stamp

var schema = Joi.object().keys({
    t: Joi.date().timestamp('unix').max(moment().unix()),
})

but I get the error that:

child "t" fails because ["t" must be less than or equal to "Sun Jan 18 1970 07:35:17 GMT+0330 (IRST)"]

I'm sure that the moment().unix() returns the current timestamp, but here it is casted to string.

like image 462
Salar Avatar asked Oct 29 '22 14:10

Salar


1 Answers

It seems that max() and min() functions can do the trick but they only work if the threshold is specified in milliseconds.

t: Joi.date().timestamp('unix')
.max(moment().unix() * 1000)
.min(moment().subtract('42', 'weeks').unix() * 1000),
like image 69
Amin Fazlali Avatar answered Nov 22 '22 20:11

Amin Fazlali