Search code examples
mongodbschemajsonschema

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! :)


Solution

  • 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
            }
          ]
        
     })