Storing array of distinct objects using simple-schema in meteor


#1

I’m right now trying to implement an array of objects using simple-schema in meteor. Each of these objects will be rendered as an option in the front end. (Something like a multi-select or “+ / -” option. Selecting each option must create a new object, the catch here is that if I select one option, I must not be able to select the same option again.(something like a mutual exclusion)

Simple-Schema:

> [...]
>  "qos.type.$.name" :{
>         type: String,
>         allowedValues: ['val1','val2'],
>         optional: true
>     },
> [...]

Current front end rendered by autoForm:

To reiterate, my question is - How do I allow only 2 objects to be added. and how do I make sure the same option isn’t selected in both the options.


#2

I figured out the first part of my question. A way to store only 2 objects is to first define the maxCount on the array.

So my schema basically is now:

[...]
 "qos.type": {
        type: Array,
        **maxCount: 2**
    },
    "qos.type.$":{
        type: Object,
    },
    "qos.type.$.name":{
        type:String,
        allowedValues: ['val1,'val2'],
        optional: true
    },
    "qos.type.$.textsnippet":{
        type: String,
        optional: true
    },   
[...]

What remains to be seen is how to check for distinct objects.


#3

I don’t know simple schema (new to meteor), but if you want to hand code the check (which would probably be faster), it’s a one liner with lodash. _.some(array, [‘key’, ‘unique value’])

I did notice that the Meteor Guide cautions against collections (arrays of objects) because of DDP overhead. They recommend a new collection.