【问题标题】:Meteor Simple Schema validating objectMeteor 简单模式验证对象
【发布时间】:2017-06-24 16:59:16
【问题描述】:

我已经使用 smpl-schema 为流星 mongo 集合定义了一个模式,但我遇到了一些令人困惑的行为。我正在尝试定义 ObjectsArray 验证正常,但插入失败。

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"
    }
  ]
});

validate 方法验证良好。但是当我插入时

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

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

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

【问题讨论】:

  • 您是否将架构附加到某处的集合中?
  • 是的,resourceCollection.attachSchema(Schemas.resourceCollectionSchema);

标签: mongodb validation meteor


【解决方案1】:

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

所以这应该可行:

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);

【讨论】:

    猜你喜欢
    • 2019-09-26
    • 2023-03-05
    • 2014-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-11
    • 2014-08-29
    相关资源
    最近更新 更多