[英]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).更具体地说,我想验证一个字段以确保它小于(“现在”+ 12 小时)或(“其他字段”+ 12 小时)。
How can I do this?我怎样才能做到这一点?
You can use Joi.ref("someField")
to refer other fields in less conditon.您可以使用
Joi.ref("someField")
在较少条件下引用其他字段。 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.希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.