Meteor-Community-Packages / meteor-collection2

A Meteor package that extends Mongo.Collection to provide support for specifying a schema and then validating against that schema when inserting and updating.
https://packosphere.com/aldeed/collection2
MIT License
1.02k stars 108 forks source link

Validation failing, but appears correct according to docs. #365

Closed thatgibbyguy closed 6 years ago

thatgibbyguy commented 7 years ago

I have defined a schema for a meteor mongo collection using smpl-schema and I'm getting some confusing behavior. I'm trying to define an Array of Objects which validates fine, but on insert fails.

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

The validate method validates fine. But when I insert

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

I get Error: After filtering out keys not in the schema, your object is now empty

How could validate pass but insert fail?

aldeed commented 6 years ago

Closing old issues. Please comment if this is still an issue and should be reopened.