Skip to content

Conditional validation of object properties with different parent key #1030

@robs0n

Description

@robs0n

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?

Metadata

Metadata

Assignees

Labels

bugBug or defect

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions