How to do conditional schema validation?

Hi, how can I achieve following?

db.createCollection("orders", {
   validator: {
      $jsonSchema: {
         bsonType: "object",
         required: [ "orderType" ],
         properties: {
            orderType: {
               enum: [ "online", "in-store" ],
               description: "must be either 'online' or 'in-store'"
            },
            shippingAddress: {
               $cond: {
                  if: { $eq: [ "$orderType", "online" ] },
                  then: { bsonType: "object", required: [ "street", "city", "state", "zip" ] },
                  else: { bsonType: "object" }
               }
            },
            storeLocation: {
               $cond: {
                  if: { $eq: [ "$orderType", "in-store" ] },
                  then: { bsonType: "object", required: [ "storeId", "storeName" ] },
                  else: { bsonType: "object" }
               }
            }
         }
      }
   }
})

Can you make the question you are asking clearer?

I want to do conditional validation using my schema. So if a field has certain value (say A) then I need validator to ensure certain fields (say 1,2,3 ) are present. If the value is B then I need the validator to ensure fields 4,5,6 are present - 1,2,3 re optional.

So, in the example I provided earlier, if the orderType has a value online, the the shippingAddress object needs to have required fields street, city, state and zip. If the orderType has some other value, then shippingAddress can be an empty object.

Yes, you are now clear, @cloudsurfer22_N_A … An interesting requirement!

Maybe you can do this using the oneOf keyword where each supplied jsonSchema object in the array is an arbitrarily deeply nested schema.