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.
null with Joi.any() and then....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!