Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

273
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda