1

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!

WalterB
  • 110
  • 2
  • 12
  • 1
    You have to use extend to combine two schemas. Eg - ```MainSchema.extend(SubSchema);``` – blueren May 22 '17 at 12:19
  • @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 May 22 '17 at 12:30

2 Answers2

1

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
  }
});
adamtheray
  • 21
  • 1
0
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.

John Smith
  • 1,559
  • 1
  • 12
  • 18
  • Thank you so much for your reply John. Unfortunately I receive the following error when using your code: 'Uncaught Error: Invalid definition for books field: "type" may not be an array. Change it to Array.'. If I understood correctly, these [brackets] should no longer be used since AutoForms 6.0 and collection2-core. See e.g. https://github.com/aldeed/meteor-simple-schema#combining-simpleschemas. Thanks again! – WalterB May 22 '17 at 12:29
  • What I said might no longer be true, indeed. I used SimpleSchema for my previous project, which was a while ago. – John Smith May 22 '17 at 12:35