1

i'm using Meteor with collection2 and I have an array that looks like this:

productTypes = ["string1", "string2", "string3", {Other: "test"}]

Essentially there will be anywhere from 0 to 7 strings in the array, and Other: 'Test' may or may not be present

So i'm trying to make a schema that handles that case. Is there a way to tell it that there will be strings and an object within an array?

I've tried

const residentSchema = new SimpleSchema({
  productTypes: {type: [String, Object], optional: true, blackbox: true},
})

But that obviously won't work because it's expecting one String and one Object. Does anyone know how I can make this work? Thanks in advance

Edit:

I am now storing it with this format:

productTypes = { list: ["string1", "string2", "string3"], Other: "test"}

but when I add a schema like this:

const productTypeSchema = new SimpleSchema({
  list: {type: Array},
  other: {type: String}
})

const residentSchema = new SimpleSchema({
  productTypes: {type: productTypeSchema},
})

My App crashes. When I remove the line list: {type: Array} it is fine.

Is Array now allowed as a value of SimpleSchema?

ruevaughn
  • 1,319
  • 1
  • 17
  • 48
  • Also, can anyone explain why I can't use {type: Array, blackbox: true} ? Whenever I use that it blows my App up, I think there is a concept here that i'm missing – ruevaughn Jun 24 '16 at 00:15

1 Answers1

1

With collection2 you can have an array of primitives or an array of objects but not a mixed array. Just from a modeling pov it's really messy.

I suggest you refactor your array, instead of:

productTypes = ["string1", "string2", "string3", {Other: "test"}]

for example:

productTypes = { list: ["string1", "string2", "string3"], Other: "test"}
Michel Floyd
  • 18,793
  • 4
  • 24
  • 39
  • Thanks that is a great idea, I was already deciding I probably needed to do something like that. Since it was so hard to do, I figured there was a reason it wasn't supported. – ruevaughn Jun 24 '16 at 06:28
  • Just wanted to note, that by switching to productTypes = { list: ["string1", "string2", "string3"], Other: "test"} it actually ended up cleaning up my code a lot. Thanks again – ruevaughn Jun 26 '16 at 07:19
  • Also, now that I switched it over I am having the same issue with declaring an Array in Simple Schema. Could you look at it? See my edit. – ruevaughn Jun 26 '16 at 07:22
  • You should declare `list: {type: [String]}` to indicate that `list` is an array of strings. – Michel Floyd Jun 27 '16 at 00:32