猫鼬模型限制

问题描述 投票:0回答:1

我有以下模型,我希望cartaoCidadao确切有8个数字。正则表达式不允许小于8,但允许大于8。出了什么问题?我还希望密码至少包含8个字符。

ar userSchema = new Schema({
  cartaoCidadao: {
    type: String,
    required: [true, "cartaoCidadao is a required field"],
    index: true,
    unique: true,
    match: /[0-9]{8}/,
  },
  password: { type: String, required: ["password is a required field"] },
  role: { type: String, enum: ["Admin", "Technical", "User"] },

  estado: { type: String, enum: ["Infetado", "Suspeito", "Não Infetado"] },
});
node.js express mongoose model
1个回答
0
投票

尝试一下

{
    cartaoCidadao: {
        //Match exactly 8 digits
        match: /^[0-9]{8}$/,
    }
    password: {
        //Any character, at least 8
        match: /.{8,}/,
        //or At least 8 characters in the list
        match: /^[0-9A-za-z]{8,}$/,
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.