【问题标题】:Mongoose - array of enum stringsMongoose - 枚举字符串数组
【发布时间】:2021-01-21 13:16:10
【问题描述】:

我有一个 Schema,它的属性是预定义的字符串数组类型。

这是我尝试做的:

interests: {
    type: [String],
    enum: ['football', 'basketball', 'read'],
    required: true
}

问题是,当我尝试输入未在枚举上定义的错误值时,它不会使用枚举列表对其进行验证。

例如,这会通过它不应该通过:

{ "interests": ["football", "asdf"] }

因为"asdf" 没有在枚举列表中预定义,所以它不应该通过验证,但不幸的是,它通过了验证并保存了它。

我试图用字符串类型的值而不是字符串数组来检查这个东西,它可以工作。

例如:

interests: {
    type: String,
    enum: ['football', 'basketball', 'read'],
    required: true
}

例如,这是按预期失败的:

{ "interest": "asdf" }

总之,我需要一个带有字符串数组类型的模式属性,它可以根据预定义的值检查它的元素

实现这个目标最有效的方法是使用 validate 方法还是有更好的方法?

【问题讨论】:

    标签: node.js arrays mongodb mongoose enums


    【解决方案1】:

    使用 ref 与定义的枚举模式建立关系

    const actionsEnums = new Mongoose.Schema({value: { type: String, enum:["account-deletion","account-update"]}});
    
    const Privilege = new Mongoose.Schema(
      {
        userLevel: { type: String, required: true },
        actions: [{type: String, refs: actionsEnums}],
    })
    

    【讨论】:

      【解决方案2】:

      这里的分发器是distributerObj的数组,同样你可以定义任何类型的对象。

      const distributerObj = new Schema({
          "dis_id": {
              "type": "String"
          },
          "status": {
              "type": "String"
          }
      });
      const productSchema = new Schema({
          "distributers": {
              "type": [distributerObj]
          }
      });
      

      【讨论】:

        【解决方案3】:

        引用:https://github.com/Automattic/mongoose/issues/6102#issuecomment-364129706

        const SubStrSz = new mongoose.Schema({ value : { type : String, enum : ['qwerty', 'asdf'] } }); const MySchema = new mongoose.Schema({ array: [SubStrSz] });

        使用该技术,您将能够验证数组内的值。

        【讨论】:

        • 所以我尝试使用您的解决方案,但不幸的是没有运气,这是我尝试做的:javascript const interestSchema = new mongoose.Schema({ value: { type: String, enum: ['football', 'basketball', 'read'] } }); var profileSchema = new mongoose.Schema({ interests: { type: new mongoose.Schema({ array: [interestSchema] }) } }) 这是我的输入 json:javascript { "interests": { "array": [{ "value": "football" }, { "value": "asdf" }] } } 它仍然没有验证@ 987654324@
        • 难道你不需要用 Schema 来包装 value 的对象吗?
        • 这是:const interestSchema = new mongoose.Schema({ value: { type: String, enum: ['football', 'basketball', 'read'] } }); 不是吗?请尝试在文本编辑器上美化此代码,因为我无法在此处的 cmets 上以更好的方式编写它
        【解决方案4】:

        您可以尝试自定义验证吗?像这样

        const userSchema = new Schema({
          phone: {
            type: String,
            validate: {
              validator: function(v) {
                return /\d{3}-\d{3}-\d{4}/.test(v);
              },
              message: props => `${props.value} is not a valid phone number!`
            },
            required: [true, 'User phone number required']
          }
        });

        这是文档: https://mongoosejs.com/docs/validation.html

        【讨论】:

          猜你喜欢
          • 2022-12-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-07-07
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多