根据我在文档中的理解,您可以像这样定义您的架构:
MySchema = new SimpleSchema({
//This says that the addresses key is going to contain an array
addresses: {
type: [Object],
},
// To indicate the presence of an array, use a $:
"addresses.$.street": {
type: String,
},
"addresses.$.city": {
type: String,
}
});
好的,我得到了这个部分。但是如果我想验证特定数组索引中的内容呢?我想要这样的东西:
MySchema = new SimpleSchema({
//This says that the itemsOrdered key is going to contain an array
itemsOrdered: {
type: [Object],
},
// Here I want to validate certain indexes in the array.
"itemsOrdered.0.sku": {
type: String
},
"itemsOrdered.0.price": {
type: Number
},
"itemsOrdered.1.sku": {
type: String
},
"itemsOrdered.1.price": {
type: Number
},
"itemsOrdered.1.quantity": {
type: Number
},
"itemsOrdered.2.sku": {
type: String
},
"itemsOrdered.2.price": {
type: Number
},
"itemsOrdered.2.customerNotes": {
type: String
optional: true
}
});
所以这里我试图验证数组索引0,1和2中的值。每个数组索引都有一个已订购的不同项。
通常我会使用哈希表数据结构,但为此我需要保留顺序,这就是我使用数组的原因。
当我尝试运行此代码时出现错误无法读取未定义的属性'blackbox'
答案 0 :(得分:4)
您是否考虑过自定义验证? https://github.com/aldeed/meteor-simple-schema/blob/master/README.md#custom-validation
根据函数中的文档,key
的{{1}}属性将提供您想要的信息。所以你可以有类似的东西:
this
这里我将验证逻辑放在MySchema = new SimpleSchema({
//This says that the itemsOrdered key is going to contain an array
itemsOrdered: {
type: [Object],
},
// Here I want to validate certain indexes in the array.
"itemsOrdered.$.sku": {
type: String,
custom: function () {
var key = this.key,
re = /\d+/;
var index = Number(key.match(re)[0]);
// Do some custom validation
}
},
"itemsOrdered.$.price": {
type: Number
},
"itemsOrdered.$.quantity": {
type: Number,
optional: true
},
"itemsOrdered.$.customerNotes": {
type: String,
optional: true
}
});
字段中,因为它是必需的。