Meteor-Community-Packages / meteor-collection2

A Meteor package that extends Mongo.Collection to provide support for specifying a schema and then validating against that schema when inserting and updating.
https://packosphere.com/aldeed/collection2
MIT License
1.02k stars 108 forks source link

Support default schema with multiple selector schemas #392

Closed ramijarrar closed 5 years ago

ramijarrar commented 5 years ago

Rebases the original PR by @rcsandell to be mergeable against recent changes.

Have also cleaned up garbage files (.swp and redundant package lock) as well as simplifying README instructions and squashing commits.

Original description:

This adds support for having a base (default) schema while also using multiple selector schemas.

There are four behaviors for attaching schemas:

  • Attach schema without selector. This will extend the base schema and any attached selector schemas.
  • Attach schema with selector. If the given selector schema exists, extends the found schema with any new fields. Or add an additional schema that matches the selector only. The new selector schema will first be extended by the base schema, if the base schema is already specified.
  • Replace schema without selector. This replaces the base schema and removes any attached selector schemas. If you want to continue to use the original selector schemas on the collection they must be reattached.
  • Replace schema with selector. This replaces the schema with the same selector with the new schema fields extended by the base schema, if the base schema is already specified. Othewise adds the new selector schema if it was not found.

It is possible to use schemas on collections with or without the base schema specified, and with or without selector schemas specified. The selector schemas will always inherit the base schema. Replacing the base schema effectively serves as deleting all schemas off the collection.

Before I started the tests were producing error: "1) extending a schema with a selector after attaching it, collection2 validation respects the extension" and that error is still being produced.