admin管理员组文章数量:1432385
I have created a Schema model in Mongoose, which has several properties, including the following shown below.
The problem with all this is that the properties: name, description and countries, ONLY ONE of them, should be required, and not all three of them.
That is to say, if I make a PUT of this model, and I don't put any property, the model is NOT valid, but, if I put one of them, the model is (or if I put two, or even three of them).
However, the required here is not valid, since it implies to add three properties.
I've tried with required, validate or Mongoose's own hooks, but none of it has worked.
const example = new Schema({
name: {
type: String,
required: true,
unique: true
},
description: String,
countries: {
type: [
{
type: String,
}
],
},
email: {
type: String
},
sex: {
type: String
},
});
I hope that with the required, I will always require the three properties
I have created a Schema model in Mongoose, which has several properties, including the following shown below.
The problem with all this is that the properties: name, description and countries, ONLY ONE of them, should be required, and not all three of them.
That is to say, if I make a PUT of this model, and I don't put any property, the model is NOT valid, but, if I put one of them, the model is (or if I put two, or even three of them).
However, the required here is not valid, since it implies to add three properties.
I've tried with required, validate or Mongoose's own hooks, but none of it has worked.
const example = new Schema({
name: {
type: String,
required: true,
unique: true
},
description: String,
countries: {
type: [
{
type: String,
}
],
},
email: {
type: String
},
sex: {
type: String
},
});
I hope that with the required, I will always require the three properties
Share Improve this question asked Sep 11, 2019 at 15:34 eolxsieolxsi 712 bronze badges2 Answers
Reset to default 7You can use custom function as the value of the required property.
const example = new Schema({
name: {
type: String,
required: function() {
return !this.description || !this.countries
},
unique: true
},
description: String,
countries: {
type: [
{
type: String,
}
],
},
email: {
type: String
},
sex: {
type: String
},
});
I doubt that there is a built-in way to achieve this specific type of validation. Here's how you could achieve what you want using the validate
method:
const example = new Schema({
name: {
type: String,
unique: true,
validate() {
return this.name || this.countries && this.countries.length > 0 || this.description
}
},
description: {
type: String,
validate() {
return this.name || this.countries && this.countries.length > 0 || this.description
}
},
countries: {
type: [String],
validate() {
return this.name || this.countries && this.countries.length > 0 || this.description
}
}
});
It will be called for all three fields in your schema, and as long as at least one of them is not null, they will all be valid. If all three are missing, then all three will be invalid. You can also tune this to fit some more specific needs of yours.
Note that this works because the context (the value of this
) of the validate method refers to the model instance.
Edit: better yet, use the required method, which basically works in the same way, as pointed out in the other answer.
本文标签: javascriptIs it possible to have at least one property required in MongooseStack Overflow
版权声明:本文标题:javascript - Is it possible to have at least one property required in Mongoose? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745594591a2665398.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论