joi icon indicating copy to clipboard operation
joi copied to clipboard

Can't get reference value when Joi.ref put inside Joi.object

Open dapapkung opened this issue 6 years ago • 3 comments

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:

image

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:

image

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

image

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}
}

dapapkung avatar Aug 27 '20 07:08 dapapkung

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').

Marsup avatar Aug 27 '20 08:08 Marsup

Thank you @Marsup !

dapapkung avatar Aug 27 '20 09:08 dapapkung

how do you use it

vhiz avatar Jul 01 '22 15:07 vhiz