AutoForm/SimpleSchema/Collection2 and reactive sub-schemas

I am trying to take advantage of the fact that MongoDB as a document database does not have a schema, which allows me to store documents with slightly different properties.

The scenario is that I want to have a collection, eg. Pets, that lists all my pets which share name and birthdate. However, one is a dog, the other a bird and the third a lizard. Those different species have different sub-properties (and no hate about my loose definition of species, please :slight_smile: )

The problem is to have the trifecta of autoform, simpleschema and collection2 handle this properly, which means form creation and validation.

In my Schema.Pet I made up the new type Schema to denote that any of the allowedValues is a proper schema. Unfortunately this doesn’t work.

Does anybody have an idea how to handle this?

Schema.Reptile = new SimpleSchema({
 length: {
  type: Number;
 },
 venomous: {
  type: Boolean;
 }
});
Schema.Mammal = new SimpleSchema({
 legs:{
  type: Number,
  allowedValues: [2,4],
 }
});
Schema.Bird = new SimpleSchema({
 carnivorous: {
  type: Boolean;
 }
});
Schemas.Pet = new SimpleSchema({
 name: {
  type: String,
 },
 kindof: {
  type: Schema,
  allowedValues: [ Schema.Reptile, Schema.Mammal, Schema.Bird ]
 },
});
Pets.attachSchema(Pet);

I also filed a github issue

1 Like

Hah! I just started thinkin on a very similar problem today with the exact same stack. Please update this as you go along. So will I. In my case, I’m preparing an exam that’s got questions, but questions can have different structures (multiple choice, fill in the blanks etc) while sharing some common mandatory properties. I’m trying to figure out a way to fit this into a single monolithic schema and a single form.

Turns out simpleschema has type SimpleSchema. Check the github issue

1 Like

Thanks, and http://autoform.meteor.com/fieldvalues also helps designing the form to display sub-schema fields based on higher level imput.

Also the part in simple schema documentation about type

Or it can be a constructor function like Date or any custom object.)

is I guess stronger and more meaningful than it seems to be.