简体   繁体   中英

How to validate Joi date based on a relative date?

I understand that you can do this to validate a date that it should be before the current datetime:

const schema = Joi.object().keys({
    date: Joi.date().less("now").iso().required()
})

However, I would like to take it further and validate a field based on a relative date either to "now" or to another field.

More spefically, I would like to validate a field to make sure that it is less than ("now" + 12 hours) or ("other field" + 12 hours).

How can I do this?

You can use Joi.ref("someField") to refer other fields in less conditon. If you need to modify something then you could create a custom function like this

Joi.date().less(Joi.ref('someField', {"adjust": someField => return someField + 1}));

adjust function basically takes incoming refered field's value and returns the modified value.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM