最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is it possible to have at least one property required in Mongoose? - Stack Overflow

programmeradmin1浏览0评论

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 badges
Add a ment  | 

2 Answers 2

Reset to default 7

You 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.

发布评论

评论列表(0)

  1. 暂无评论