1
votes

I am trying to get subschemas to work as an array, which I assume is the correct way to handle my problem (but please correct me if I am wrong!). I provide a simplified working example to show my problem, based on the BooksSchema example provided by the AutoForm package. In my example, I have a collection of Libraries, and one of the fields in the 'Libraries' object is supposed to be the library's collection of books. Rendering the AutoForm does not give me any input labels as defined in my Book collection, but instead just shows one (1) empty text input field.

Schemas:

import SimpleSchema from 'simpl-schema';
SimpleSchema.extendOptions(['autoform']);

BooksSchema = new SimpleSchema({
  title: {
    type: String,
    label: "Title",
    max: 200
  },
  author: {
    type: String,
    label: "Author"
  },
  copies: {
    type: Number,
    label: "Number of copies",
    min: 0
  },
  lastCheckedOut: {
    type: Date,
    label: "Last date this book was checked out",
    optional: true
  },
  summary: {
    type: String,
    label: "Brief summary",
    optional: true,
    max: 1000
  }
}, { tracker: Tracker });

LibrariesSchema = new SimpleSchema({
  collection: {
    type: Array
  },
  'collection.$': {
      type: BooksSchema,
      minCount: 1
  }
});

LibrariesSchema.extend(BooksSchema);


Libraries = new Mongo.Collection("libraries");
Libraries.attachSchema(LibrariesSchema);

AutoForm:

  {{> quickForm collection="Libraries" id="insertBookForm" type="insert"}}

Thank you so much in advance for your time, really been struggling with this for a long time now!

2
You have to use extend to combine two schemas. Eg - MainSchema.extend(SubSchema);blueren
@blueren Thank you very much for your time. I updated my code to include this 'extend' (see edited question), but it unfortunately did not result in any changes for me.WalterB

2 Answers

1
votes

In my case I was indeed able to resolve the issue by using John Smith's example without the brackets.

LibrariesSchema = new SimpleSchema({
  'books': {
      type: BooksSchema,
      minCount: 1
  }
});
0
votes
LibrariesSchema = new SimpleSchema({
  'books': {
      type: [BooksSchema],
      minCount: 1
  }
});

Arrays of a specific types, for use in check() or schema definitions, are specified as [SomeType], eg. [String], or [BooksSchema] in your case.