简体   繁体   English

如何使用 Joi 验证实现正确的参考逻辑?

[英]How can I implement proper reference logic with Joi validation?

I'm trying to create a simple schema that will check to make sure that a number range is correctly inputted.我正在尝试创建一个简单的模式,该模式将检查以确保正确输入了数字范围。 Logic is that start must be less than end and end must be greater than start .逻辑是start必须小于end并且end必须大于start

Here is my schema;这是我的架构;

const start = Joi.number().less(Joi.ref('end'))
const end = Joi.number().greater(Joi.ref('start'))
//age Range object
const ageRange = Joi.object().keys({
  start: start,
  end: end,
})

The error I'm getting while the code is compiling: Error: item added into group end created a dependencies error我在编译代码时遇到的错误: Error: item added into group end created a dependencies error

I've tried changing the logic but I'm not sure what other structure would work.我试过改变逻辑,但我不确定其他结构会起作用。 I get that It's possibly an issue with the reference logic so any alternative suggestion to this would be greatly appreciated.我知道这可能是参考逻辑的问题,因此任何对此的替代建议都将不胜感激。

start must be less than end and end must be greater than start I think you can just check one of them, the other one is true automatically. start 必须小于 end 并且 end 必须大于 start我想你可以只检查其中一个,另一个是自动的。 not sure what the terms was in Mathematics.不确定数学中的术语是什么。

something like this should work这样的事情应该工作

 const schema = { start: Joi.date().iso().required(), end : Joi.date().iso().greater(Joi.ref('start')).required() }; let ageRange = Joi.object().keys(schema);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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