0

I am using the npm version of Simple Schema. I have a schema defined like this:

const PageSchema = new SimpleSchema({
  organizationId: String,
  title: String,
  published: Boolean,
  slug: String,
  content: {
    type: Object,
    optional: true
  }
})

The 'content' value is meant to contain data exported from Draft JS using Draft's convertToRaw function. However, when I clean the data using Simple Schema, the content key/value gets completely removed from the object. No errors are thrown, it just quietly deletes that node.

I certainly did not expect a side-effect like this. Is there something I'm not understanding about Simple Schema's 'Object' type? Does it expect me to serialize an object as JSON first, or something? Or maybe there is something about the object literal exported by Draft JS that it doesn't like...?

Or is it possible Draft JS is doing something in its object export that Simple Schema finds unorthodox? Is this a Draft JS problem or a Simple Schema problem?

Here's a console.log of the data before cleaning:

{ organizationId: 'JEsvMiJeTgexkAuzH',
  title: 'Test Title',
  published: true,
  slug: 'test-title',
  content: { entityMap: {}, blocks: [ [Object] ] } }

And here's what it looks like after the cleaning:

{ organizationId: 'JEsvMiJeTgexkAuzH',
  title: 'Test Title',
  published: true,
  slug: 'test-title' }

Any pointers/help would be greatly appreciated! I have not attempted to use object literals as values before when working with meteor data, so I'm sure there's just some fundamental thing I'm missing.

UPDATE: I tried inserting the data without calling clean() first, and this actually works just fine. However, this is, of course, a far-from-ideal workaround. But it does tell me there isn't anything about the data that Mongo or meteor collections object to.

Dave Munger
  • 479
  • 3
  • 11

1 Answers1

1

I figured it out, and it's just that I somehow missed something very significant in the Simple Schema documentation:

If you have a key with type Object, the properties of the object will be validated as well, so you must define all allowed properties in the schema. If this is not possible or you don't care to validate the object's properties, use the blackbox: true option to skip validation for everything within the object.

Until such time as I determine the keys and contents I expect consistently from Draft JS, I am just setting blackbox to true for that value, like so, since I am basically okay with trusting Draft JS to provide the correct object keys:

const PageSchema = new SimpleSchema({
  organizationId: String,
  title: String,
  published: Boolean,
  slug: String,
  content: {
    type: Object,
    blackbox: true,
    optional: true
  }
})

This solved my problem, and now the node containing the Draft JS exported content object is not deleted on calling Simple Schema clean.

Dave Munger
  • 479
  • 3
  • 11