gpt4 book ai didi

JSON 架构 : How to check that an array contains at least one object with a property with a given value?

转载 作者:行者123 更新时间:2023-12-04 17:15:54 24 4
gpt4 key购买 nike

如何在以下 json 中检查数组中至少有一个元素 names有房产nickNameGinny ?

{
"names": [
{
"firstName": "Hermione",
"lastName": "Granger"
}, {
"firstName": "Harry",
"lastName": "Potter"
}, {
"firstName": "Ron",
"lastName": "Weasley"
}, {
"firstName": "Ginevra",
"lastName": "Weasley",
"nickName": "Ginny"
}
]
}

目前我使用的是draft-06 版本(FAQ here)。

这是我的 NOT WORKING 架构:
{
"$schema": "http://json-schema.org/draft-06/schema#",
"title": "Complex Array",
"description": "Schema to validate the presence and value of an object within an array.",

"type": "object",
"properties": {
"names": {
"type": "array",
"minItems": 1,
"items": {
"type": "object",
"properties": {
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
},
"nickName": {
"type": "string"
}
},
"anyOf": [
{"required": ["nickName"]}
]
}
}
}
}

最佳答案

我设法使用 draft-06 弄清楚了.在此版本中新增关键字 contains加入。根据这个草案specification :

contains

The value of this keyword MUST be a valid JSON Schema. An array instance is valid against "contains" if at least one of its elements is valid against the given schema.



工作架构:
{
"$schema": "http://json-schema.org/draft-06/schema#",
"title": "Complex Array",

"type": "object",
"properties": {
"names": {
"type": "array",
"minItems": 1,
"contains": {
"type": "object",
"properties": {
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
},
"nickName": {
"type": "string",
"pattern": "^Ginny$"
}
},
"required": ["nickName"]
},
"items": {
"type": "object",
"properties": {
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
},
"nickName": {
"type": "string"
}
}
}
}
}
}

关于JSON 架构 : How to check that an array contains at least one object with a property with a given value?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47141845/

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