如何验证猫鼬模式中字符串数组的最大长度?

时间:2021-04-26 10:40:54

标签: javascript node.js mongodb mongoose

int

我的验证功能不起作用。如何验证我的状态字符串数组。我想验证每个 20 个字符的字符串

1 个答案:

答案 0 :(得分:0)

你可以这样试试:

const postschema = new Schema({
userName :{
    type : String,
    required : true
},
  status: {
    type: [{
      type: Schema.Types.ObjectId,
      ref: 'peopleModel'
    }],
    validate: [arrayLimit, '{PATH} exceeds the limit of 10']
  }
});

function arrayLimit(val) {
  return val.length <= 10;
}