simpl-schema: Not possible to have simpl-schema in two locations in node_modules (_constructorOptions key is missing "type")

In that situation I get the following when trying to Collection.attachSchema: "_constructorOptions" key is missing "type"

So I think an existing instance of SimpleSchema is being treated as an object literal schema because the instanceof has failed because the schema was created in a node_modules/simpl-schema at a different path. That’s my guess anyway.

So currently this means I can’t debug my npm packages that also use SimpleSchema.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 21

Commits related to this issue

Most upvoted comments

@aldeed Gentle request. Would be great if you could look into the PR for this!

@aldeed: Waiting for the fix, PR does resolves the issue. Community will be glad to see it approved and merged

@aldeed I created the PR.

@damonmaria @dpankros @faisalhasnain @SantoshSrinivas79 and others, merged PR and released 0.3.0. Please verify this fixes and close this issue if so. Thanks!

So this error probably comes up in multiple situations with a slightly different messages. The original message _constructorOptions key is missing "type" comes is thrown when a schema is attached to a collection E.g. const c = new Mongo.Collection('c_name'); c.attachSchema(MySchema); In this case, the if (!(ss instanceof SimpleSchema)) { is not recognizing the schema.

To address this, I refactored all the schema instanceof SimpleSchema calls into a common SimpleSchema.isSimpleSchema(val) so I could centralize the check to one place. I have verified over and over that I don’t have the old aldeed:simple-schema package loaded at runtime (or in any of my deps). I added the duck typing check that I mentioned previously to the isSimpleSchema method. @aldeed if you want me to send a PR for this, let me know.

With that in place, I receive an error that says 'prop.$' key is missing "type". Look familiar? (!)

This error is thrown in a different place and in schemas where I specify a custom class as the type, generally as an array or map. It’s not a second SimpleSchema floating around. It’s whenever I specify a custom class. Note: CustomModel is NOT a subschema. For example:

 prop: {
    type: Array,
    blackbox: true,
    optional: true,
  },
  'prop.$': {
    type: CustomModel,  // <-- this triggers the error
    blackbox: true,
    optional: true,
  },

For now I’m just switching them to Object as that seems to bypass the error. E.g.:

 prop: {
    type: Array,
    blackbox: true,
    optional: true,
  },
  'prop.$': {
    type: Object,
    blackbox: true,
    optional: true,
  },

Hope that helps someone.