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

Conditional validation of object properties with different parent key #1030

Closed
robs0n opened this issue Nov 8, 2016 · 3 comments
Closed
Assignees
Labels
bug Bug or defect
Milestone

Comments

@robs0n
Copy link

robs0n commented Nov 8, 2016

I'm trying to make sure, that the numeric value of an object property is not lesser than another numeric object property under a different parent key. I tried to use object.assert for this, but i did not get the expected results.

  • node version: 4.4.7
  • joi version: 9.2.0
  • environment: node
const Joi = require('joi');

const schema = Joi.object().keys({
  a: Joi.object().keys({
    b: Joi.number()
  }),
  c: Joi.object().keys({
    d: Joi.number()
  })
}).assert('a.b', Joi.number().min(Joi.ref('c.d')), '"a.b" cannot be less than "c.d"');

const results = Joi.validate({
  a: {
    b: 2
  },
  c: {
    d: 1
  }
}, schema);

console.log(results);

Although a.b is greater than c.d, i get a validation error.
What am i doing wrong?

@Marsup Marsup added the bug Bug or defect label Nov 9, 2016
@Marsup Marsup self-assigned this Nov 9, 2016
@Marsup Marsup added this to the 10.0.0 milestone Nov 10, 2016
@WesTyler
Copy link
Contributor

WesTyler commented Nov 11, 2016

I've traced this down to https://github.com/hapijs/joi/blob/master/lib/number.js#L147

Looks like state is getting lost at one of the calls of https://github.com/hapijs/joi/blob/master/lib/any.js#L426 and state.parent ends up null, which results in compareTo being undefined.

Trying to figure out where state is getting dropped, but this goes several levels deeper than I've gone before and I wanted to share findings here in case something clicked for someone else faster.

@Marsup
Copy link
Collaborator

Marsup commented Nov 11, 2016

It's already fixed locally, don't bother with this one :)

@WesTyler
Copy link
Contributor

Haha ok. I'm glad I took a look because I learned a bunch about refs. Interested to see what the fix ended up being when you push it. :)

@Marsup Marsup closed this as completed in 9f6fb73 Nov 12, 2016
@lock lock bot locked as resolved and limited conversation to collaborators Jan 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Bug or defect
Projects
None yet
Development

No branches or pull requests

3 participants