joi
joi copied to clipboard
Can't get reference value when Joi.ref put inside Joi.object
Support plan
- is this issue currently blocking your project? (yes/no): yes
- is this issue affecting a production system? (yes/no): no
Context
- node version: v10.19.0
- module version: ^17.2.1
- environment (e.g. node, browser, native): browser
- used with (e.g. hapi application, another framework, standalone, ...): Joi Tester (https://joi.dev/tester/)
- any other relevant information: -
How can we help?
Hi? for example i've schema like this:

Then, i want to validate delivery.month field is must less than subscribe field. I've tried it at Joi Tester (https://joi.dev/tester/) but i got the result like this:

But, when the schema is like below, it's works properly:

Any solutions? Thank you!
Example schema & data validate, (just copy at joi tester https://joi.dev/tester/ *if you wanna try)
Schema:
//Insert your joi schema here
Joi.object({
username: Joi.string().alphanum().min(3).max(30).required(),
password: Joi.string().pattern(/^[a-zA-Z0-9]{3,30}$/),
subscribe: Joi.number().min(1).required(),
delivery: Joi.object({
month: Joi.number().min(1).max(Joi.ref('subscribe')).required(),
day: Joi.number().required()
}),
repeat_password: Joi.ref("password"),
access_token: [Joi.string(), Joi.number()],
birth_year: Joi.number().integer().min(1900).max(2013),
email: Joi.string().email({ minDomainSegments: 2, tlds: { allow: ["com", "net"] } } )
}).with('username', 'birth_year').xor('password', 'access_token').with('password', 'repeat_password')
Data To Validate:
//Insert data to validate here
{
username: "abc",
password: "password",
repeat_password: "password",
birth_year: 1994,
subscribe: 7,
delivery: {month: 10, day: 30}
}
As mentioned in the docs, references point to siblings, if it's outside of the scope you have to point at it, so in your case Joi.ref('...subscribe').
Thank you @Marsup !
how do you use it