jsonSchema working only for the first item of an array of objects

I had built an API that was working with NodeJS and express, ones I felt confident with it I learnt Mongo in order to get closer to the MERN stack. Everything is working smoothly and now, just for learning purposes, I’m trying to enforce a schema so that post and put methods are somewhat limited.

My schema works fine except for the colors_available array where the criteria that should be filtering the whole, works only for the first item of the array: (please notice that ‘type’ has been replaced with ‘bsonType’ as it’s, again, working with MongoDB)

colors_available: {
        bsonType: 'array',
        items: [
          {
            bsonType: 'object',
            additionalProperties: false,
            uniqueItems: true,
            minItems: 1,
            required: [
              'color',
              'stock'
            ],
            properties: {
              color: {
                bsonType: 'string'
              },
              stock: {
                bsonType: 'int'
              }
            }
          }
        ]
      }

As a reference, this is what a document’s colors_available field looks like: (the ‘colors_available’ array could be of any length >=1)

"colors_available": [
    {
      "color": "Green",
      "stock": 42
    },
    {
      "color:": "Black",
      "stock": 13
    },
    {
      "color": "White",
      "stock": 21
    }
  ]

I have tried removing the squared brackets at the items field but it just broke everything…

Any suggestions are more than welcome! :slight_smile:

I also replied to your SO post.

The array assertions are at the wrong level.

One thing to note is the use of additionalProperties:false causes a validation error with MongoDB schemas if you don’t define _id in your schema definition. This is because all documents are inserted with _id by default if you omit this property from your document insertion.

source: https://www.mongodb.com/docs/manual/core/schema-validation/specify-json-schema/json-schema-tips/#_id-field-and-additionalproperties--false

db.createCollection("colors", {
    validator: {
        $jsonSchema: {
            bsonType: "object",
            title: "Colors Available",
            required: ["colors_available"],
            properties: {
                "colors_available": {
                    "bsonType": "array",
                    "minItems": 1,
                    "uniqueItems": true,
                    "items": {
                        "bsonType": "object",
                        "additionalProperties": false,
                        "required": [
                            "color",
                            "stock"
                        ],
                        "properties": {
                            "_id": {
                                "bsonType": "objectId"
                            },
                            "color": {
                                "bsonType": "string"
                            },
                            "stock": {
                                "bsonType": "int"
                            }
                        }
                    }
                }
            }
        }
    }
})

The example you provided has an error in the second schema "color:". The colon should be outside of the quotes.

A valid payload for this schema follows:

db.colors.insertOne({
    "colors_available": [
        {
          "color": "Green",
          "stock": 42
        },
        {
          "color": "Black",
          "stock": 13
        },
        {
          "color": "White",
          "stock": 21
        }
      ]
    
 })
1 Like

Hey, thanks for taking time in answering my question :slight_smile:
The schema provided includes just the colors_available property of a major schema (for clarity purposes), if anyone got back to the post later, here is the full schema in case they were interested in it:

{
  $jsonSchema: {
    bsonType: 'object',
    additionalProperties: false,
    required: [
      '_id',
      'brand',
      'model',
      'price',
      'colors_available',
      'autopilot'
    ],
    properties: {
      _id: {
        bsonType: 'objectId'
      },
      brand: {
        bsonType: 'string'
      },
      model: {
        bsonType: 'string'
      },
      price: {
        bsonType: 'decimal'
      },
      colors_available: {
        bsonType: 'array',
        items: {
          bsonType: 'object',
          additionalProperties: false,
          uniqueItems: true,
          minItems: 1,
          required: [
            'color',
            'stock'
          ],
          properties: {
            color: {
              bsonType: 'string'
            },
            stock: {
              bsonType: 'int'
            }
          }
        }
      },
      autopilot: {
        bsonType: 'bool'
      }
    }
  }
}

In my case, the error was caused by the typo that you have noticed, in fact, that’s the reason that made my schema break when I tried to remove the squared brackets.

If anyone were interested, here is also a correct example of a document:

{
  "_id": {
    "$oid": "64f84820853dde3e3c6b10f2"
  },
  "brand": "Maserati",
  "model": "Granturismo",
  "price": {
    "$numberDecimal": "180000.00"
  },
  "colors_available": [
    {
      "color": "Blue",
      "stock": 2
    },
    {
      "stock": 4,
      "color": "White"
    }
  ],
  "autopilot": false
}

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.