Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

265
Views
how to check if empty or ref to another schema in JOI?

I have a "Boulder" Schema in joi:

module.exports.boulderSchema = Joi.object({
    name: Joi.string().required(),
    grade: Joi.string().required(),
    image: Joi.string().required(),
    description: Joi.string().required(),
    reviews: Joi.any()
})

with "Review" Schema:

module.exports.reviewSchema = Joi.object({
    rating: Joi.number().required().min(1).max(5),
    comment: Joi.string().required()
})

the "Boulder" mongoose schema that my Joi Schema references off is as follows:

const boulderSchema = new Schema({
    name: {
        type: String,
        required: true
    },
    grade: {
        type: String,
        required: true
    },
    image: {
        type: String,
        required: true
    },
    description: {
        type: String,
        required: true
    },
    reviews: [{type: Schema.Types.ObjectId, ref:'Review'}]
});

and likewise my "Review" mongoose Schema:

const reviewSchema = new Schema({
    rating: {
        type: Number,
        required: true
    },
    comment: {
        type: String,
        required: true
    }
});

So here's the question... When I make a "boulder" datapoint, it can have "review" attached to it, or it can have "review" being null hence my janky code of Joi.any() to accept both. I'm also checking if reviews in empty and using delete req.body['reviews']; in my post route before saving my datapoint. Ive looked into .alternatives() and .when() but im not sure how to use it.

I guess my question comes in two parts.

  1. How do I make a solid Joi Schema that will check "when" its empty to accept null with Joi.any() and then...
  2. use "alternatives" to check its .array()of objectId that matches to the reference to "Review"?

if at all this is how it should be done. im totally at lost and any help will be appreciated!

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!