cocacrave
cocacrave

Reputation: 2633

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?

Upvotes: 1

Views: 125

Answers (2)

Michel Floyd
Michel Floyd

Reputation: 20227

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.

Upvotes: 2

Lemonade
Lemonade

Reputation: 503

It should imho be

  relations : { type : [boolean] }

So then you would have

 LOVE.relations 

as a collection of say 8 entries. No need to constraint it to these 8 i assume.

You might use a different term, maybe affairs or a better match, but the idea behind is just to use it the way a collection is meant to be used for the purpose.

Upvotes: 0

Related Questions