Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

268
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda