最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin7PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far