• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

131
Views
Validation using joi with external schema

So, i'm trying to validate if a field is required given a specific scenario. Like this:

cake: Joi.object().keys({
            flavor: Joi.string().max(45).required(),
            price: Joi.string().length(2).required(),
            topping: Joi.string().length(2).required()
})

and in the same schema i have:

authentication: Joi.object().keys({
      password: Joi.string().max(45).required(),
      login64: Joi.string().max(45).required()
})

and i want to make a validation like:

authentication: Joi.object().keys({
      password: Joi.string().max(45).required(),
      login64: Joi.string().max(45).when('cake.flavor', {
      is: 'chocolate',
      then: Joi.required()

} })

i want to make login64 required if the flavor of the cake is chocolate. But nothing seems to work...

about 3 years ago · Juan Pablo Isaza
1 answers
Answer question

0

A Joy reference is by default relative to the parent of the current value, which in your case is authentication. You need to move a couple of levels up (...) in order to reach cake:

login64: Joi.string().max(45).when('...cake.flavor', {
    is: 'chocolate',
    then: Joi.required()
})
about 3 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error