I want to validate a number, which depends on other values. I tried something like the following:
var schema = {
a: Joi.number().
b: Joi.number()
c: Joi.number().max(Joi.ref('a') + Joi.ref('b'))
};
In this case c should not be greater than the value of a + b.
If I run this code, I get
Error: limit must be a number or reference
You mustn't be familiar with javascript, you can't sum objects like that.
This is currently not supported, and probably never will with that syntax, you'll have to do that another way until we can support that.
@Marsup you have to admit it's a cute attempt though...
c: Joi.number().max(Joi.ref('a') + Joi.ref('b'))
I wish I could understand, why such pattern does not seem to be valid, @Marsup. I had couple of cases, where I dreamed of such functionality. I had to do dirty workarounds instead.
Why not passing a function as a limit, next to number or reference?
Such a very welcoming atmosphere here.
Is there a recommended approach to doing this kind of validation?
@reaktivo now joi supports extensions, I think the only way is to redefine max, references haven't evolved since the time of this issue. It may be super simple or super complex depending on the features you want to support.
--
@blurpis, you could assume I was saying that with a harsh tone, or just consider I was stating a mere fact with no malice. Javascripters know that there's no such thing as operators overload and usually don't even look at mathematical operations to do stuff. There was, at the time, just no way to achieve what he wanted. Sorry if the OP felt offended by what I said.
Is there any recommended Joi approach for summation validation. I am trying to sum array's inside of numbers. But I cant sum numbers inside array with Joi.
As previously mentioned, an extension is probably your best bet.
For anyone still looking for a solution, according to @hueniverse it seems now possible with joi16: https://twitter.com/eranhammer/status/1143266728497033220
That's correct, the RC is published but you're a bit on your own as for the migration until we write a proper guide.
This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.
Most helpful comment
For anyone still looking for a solution, according to @hueniverse it seems now possible with joi16: https://twitter.com/eranhammer/status/1143266728497033220