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!