Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate a number, which depends on other validate numbers #667

Closed
SteffenGorenflo opened this issue Jun 4, 2015 · 11 comments
Closed

Validate a number, which depends on other validate numbers #667

SteffenGorenflo opened this issue Jun 4, 2015 · 11 comments
Assignees
Labels
feature New functionality or improvement
Milestone

Comments

@SteffenGorenflo
Copy link

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
@Marsup
Copy link
Collaborator

Marsup commented Jun 4, 2015

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 Marsup closed this as completed Jun 4, 2015
@Marsup Marsup added the support Questions, discussions, and general support label Jun 4, 2015
@Marsup Marsup self-assigned this Jun 4, 2015
@hueniverse
Copy link
Contributor

@Marsup you have to admit it's a cute attempt though...

@pavelmickevic
Copy link

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?

@blurpis
Copy link

blurpis commented Apr 12, 2017

Such a very welcoming atmosphere here.

@reaktivo
Copy link

reaktivo commented Mar 6, 2018

Is there a recommended approach to doing this kind of validation?

@Marsup
Copy link
Collaborator

Marsup commented Mar 6, 2018

@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.

@oguzhannkayaa
Copy link

oguzhannkayaa commented Sep 26, 2018

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.

@Marsup
Copy link
Collaborator

Marsup commented Sep 26, 2018

As previously mentioned, an extension is probably your best bet.

@SteffenGorenflo
Copy link
Author

For anyone still looking for a solution, according to @hueniverse it seems now possible with joi16: https://twitter.com/eranhammer/status/1143266728497033220

@Marsup
Copy link
Collaborator

Marsup commented Jul 1, 2019

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.

@hueniverse hueniverse added request and removed support Questions, discussions, and general support labels Jul 1, 2019
@hueniverse hueniverse self-assigned this Jul 1, 2019
@hueniverse hueniverse added this to the 16.0.0 milestone Jul 1, 2019
@hueniverse hueniverse added the v16 label Aug 10, 2019
@hueniverse hueniverse added feature New functionality or improvement and removed request labels Sep 19, 2019
@lock
Copy link

lock bot commented Jan 9, 2020

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.

@lock lock bot locked as resolved and limited conversation to collaborators Jan 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature New functionality or improvement
Projects
None yet
Development

No branches or pull requests

7 participants