gpt4 book ai didi

jsonSchema working only for the first item of an array of objects(仅适用于对象数组的第一项的jsonSchema)

转载 作者:bug小助手 更新时间:2023-10-25 19:44:36 32 4
gpt4 key购买 nike



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.

我构建了一个与NodeJS和Express协同工作的API,我对此很有信心,我学习Mongo是为了更接近MERN堆栈。一切都很顺利,现在,仅出于学习的目的,我正在尝试实施一个模式,以便POST和PUT方法受到一定的限制。


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数组,其中应该过滤整体的条件只适用于数组的第一项:(请注意,'type'已被替换为'bsonType',因为它再次与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字段的样子:('colors_available'数组可以是>=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...

我试着去掉Items栏中的方括号,但它破坏了一切……


Any suggestions are more than welcome! :)

任何建议都欢迎!:)


更多回答
优秀答案推荐

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.

需要注意的一点是additionalProperties的使用:如果在模式定义中没有定义_id,false会导致MongoDB模式的验证错误。这是因为如果在文档插入中省略此属性,则默认情况下所有文档都将使用_id插入。



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.

您提供的示例在第二个模式“COLOR:”中有一个错误。冒号应该在引号之外。


A valid payload for this schema follows:

此架构的有效有效负载如下:


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

})

更多回答

correct, if anyone is seeing this in the future and wants to have more details, further explanation and examples are provided here: Complete Solution with examples

正确,如果任何人在将来看到这一点并想了解更多细节,这里提供了更多的解释和示例:完整的解决方案和示例

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com