ORM (mongoose): ANY_OF, ALL_OF, ONE_OF oh my!

(Kmeister) #1

Hi All - Sorry for the long post.

I don’t understand how to use ANY_OF, ALL_OF, ONE_OF correctly.

We use mongoosjs for our ORM which is very flexible on its input when referencing a foreign collection.

Here is a definition example:

const Contacts = new Schema(
  {first_name: {type: String, required: true, trim: true,}},
  {collection: 'Contacts'}

const Posts = new Schema(
    description: {type: String, trim: true, required: true},
    // notice that contacts is an array of items in the foreign model Contacts
    contacts: [{
      type: Schema.Types.ObjectId,
      ref: 'Contacts',
  {collection: 'Posts'}

With mongoose you can create new posts with any of the following formats:

EXAMPLE 1) this one saves [] into post.contacts

{ description: 'hi' }

EXAMPLE 2) these 2 save [111] into post.contacts
Notice: 2.a doesn’t need an array item to validate

// 2.a
{ description: 'hi', contacts: 111 }
// 2.b
{ description: 'hi', contacts: [111]}

EXAMPLE 3) these 2 save [222] into post.contacts

// 3.a
{ description: 'hi', contacts: [{_id:222, first_name:'kris'}] }
// 3.b
{ description: 'hi',contacts: [{_id:222}] }

My best practice questions are:

  1. How much of this flexibility should we define for the users of our service?
  2. What is the best way to define the Posts model in stoplight?
  3. What of ANY_OF, ALL_OF, ONE_OF should we use?