Aldeed Simple-Schema,如何在另一个字段中使用允许的值?

Aldeed Simple-Schema, how to use allowed values in a field from another one?

我正在尝试为集合构建一种特定的简单模式,我想确保:

当用户在我的 selectsCollection 中输入新的 select 时,他会将其中一个选项值作为 selected 值。

例如:

SelectsCollection.insert({name:"SelectOne",deviceType:"Select",options:["option1","option2","option3"],value:"option4",description:"This is the first select"});

这不一定有效。我希望他只写三个选项中的一个。

这是我的架构:

SelectsCollection = new Mongo.Collection('Selects'); //Create a table

SelectsSchema = new SimpleSchema({  
    name:{      
        type: String,
        label:"Name",
        unique:true
    },  
    deviceType:{
        type: String,
        allowedValues: ['Select'],
        label:"Type of Device"
    },
    options:{
        type: [String],
        minCount:2,
        maxcount:5,
        label:"Select Values"
    },
    value:{
        type: String,
        //allowedValues:[options] a kind of syntax
        // or allowedValues:function(){ // some instructions to retrieve  the array of string of the option field ?}
        label:"Selected Value"
    },
    description:{
        type: String,
        label:"Description"
    },
    createdAt:{
        type: Date,
        label:"Created At",
        autoValue: function(){
            return new Date()
        }
    } 
});

SelectsCollection.attachSchema(SelectsSchema);

有什么想法吗? :)

非常感谢!

这可以通过字段的 custom 验证函数来完成,在此函数中您可以从其他字段中检索值:

SelectsSchema = new SimpleSchema({
  // ...
  options: {
    type: [String],
    minCount: 2,
    maxcount: 5,
    label: "Select Values"
  },
  value: {
    label: "Selected Value",
    type: String,
    optional: true,
    custom() {
      const options = this.field('options').value
      const value = this.value

      if (!value) {
        return 'required'
      }

      if (options.indexOf(value) === -1) {
        return 'notAllowed'
      }
    }
  },
  // ...
});

查看此处custom-field-validation了解更多信息