流星简单架构验证对象



我已经使用smpl-schema定义了流星Mongo集合的模式,我得到了一些令人困惑的行为。我正在尝试定义对象的 array ,但在插入失败时。

import SimpleSchema from 'simpl-schema';
const Schemas = {};
const resourceCollection = new Mongo.Collection('resourcecollection');
Schemas.resourceCollectionSchema = new SimpleSchema({
  resourceTypes: {
    type: Array,
    label: `The resources for a this collection`
  },
  "resourceTypes.$": {
    type: Object,
    blackbox: true
  },
  "resourceTypes.$.blah": {
    type: String
  }
}).validate({
  resourceTypes: [
    {
      "blah": "blah"
    }
  ]
});

验证方法验证罚款。但是当我插入

resourceCollection.insert({
  resourceTypes: [
    {
      "blah": "blah"
    }
  ]
});

我得到Error: After filtering out keys not in the schema, your object is now empty

如何验证通过但插入失败?

有一种验证方法可以验证自己一个针对预定义的模式的对象,但是在集合的情况下,您只需要附加架构本身,而不是验证的结果。

所以这应该有效:

const resourceCollection = new Mongo.Collection('resourcecollection');
Schemas.resourceCollectionSchema = new SimpleSchema({
  resourceTypes: {
    type: Array,
    label: 'The resources for a this collection'
  },
  "resourceTypes.$": {
    type: Object
  },
  "resourceTypes.$.blah": {
    type: String
  }
});
resourceCollection.attachSchema(Schemas.resourceCollectionSc‌​hema);

最新更新