Search code examples
javascriptmeteorsimple-schema

Meteor SimpleSchema Repeated Nested Objects


I'm wondering if there's a simple way of doing this. First this is my current schema:

Test.schema = new SimpleSchema({
  owner: {
    type: String,
    label: 'Owner of the test',
  },
  name: {
    type: String,
    label: 'Name of the test',
  },
  createdAt: {
    type: Date,
    label: 'Date of test creation',
  },

  // MY QUESTION IS FOR THE OBJECT BELOW

  [LOVE]: {
    type: Object,
    label: `Configuration for ${LOVE}`,
  },
  [LOVE.one]: { type: Boolean },
  [LOVE.two]: { type: Boolean },
  [LOVE.three]: { type: Boolean },
  [LOVE.four]: { type: Boolean },
  [LOVE.five]: { type: Boolean },
  [LOVE.six]: { type: Boolean },
  [LOVE.seven]: { type: Boolean },
  [LOVE.eight]: { type: Boolean },
  [LOVE.nine]: { type: Boolean },
});

Where I have the LOVE variable, I would like it to be equal to multiple values so that I don't have to write the same schema over and over.

I thought I could use something like regex but I don't know. Can someone help?


Solution

  • Just use a nested schema:

    lovers = new SimpleSchema({
      one:   {type: boolean},
      two:   {type: boolean},
      three: {type: boolean},
      four:  {type: boolean},
      five:  {type: boolean},
      six:   {type: boolean},
      seven: {type: boolean},
      eight: {type: boolean},
      nine:  {type: boolean},
    });
    

    and then reuse it in your original schema:

    Test.schema = new SimpleSchema({
      owner: {
        type: String,
        label: 'Owner of the test',
      },
      name: {
        type: String,
        label: 'Name of the test',
      },
      createdAt: {
        type: Date,
        label: 'Date of test creation',
      },
      LOVE:    { type: lovers },
      PASSION: { type: lovers },
      DESIRE:  { type: lovers },
      etc...
    });
    

    Hopefully this is what you were after.