1

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?

Michel Floyd
  • 18,793
  • 4
  • 24
  • 39
cocacrave
  • 2,453
  • 4
  • 18
  • 30
  • What regex would you like to use to recognize (one, two, three ... a.s.o) ? Maybe i got something wrong, but why not use a collection for Love Objects instead ? Or maybe there is a preprocessor that can insert according to a template, so you dont have to write it over and over ... if so ... – Lemonade Jun 29 '16 at 00:24
  • The variable LOVE should be equal to many values so I can just write that part once. The keys one, two, three and so on is always same for every different values of LOVE. My bad, I hope that was more clearer. – cocacrave Jun 29 '16 at 00:29

2 Answers2

2

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.

Michel Floyd
  • 18,793
  • 4
  • 24
  • 39
  • 1
    Thank you for this example, I'm glad I came here. [Here is a link to the documentation](https://github.com/aldeed/meteor-simple-schema) for those coming in the future. – Jeremy Iglehart Jun 29 '16 at 06:10
0

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.

Lemonade
  • 503
  • 2
  • 8