我想要实现的JSON模式可以将name下的属性约束到一个列表中,作为其他代码,我希望输入name,name对象的人的名字应该是[quot;TOM",quot;JACK"LILY">
{
"NAME":{
"TOM": "male",
"JACK": "male"
}
} #This object meets the requirements
{
"NAME":{
"TOM": "male",
"LILY": "female"
"BRUCE": "male"
}
} #This object meets the requirements
{
"NAME":{
"JERRY": "male",
"LILY": "female"
"BRUCE": "male"
}
} #This object not meets the requirements beacuse JERRY not in the list.
{
"type": "object",
"required": ["NAME"],
"properties": {
"NAME": {
"type": "object",
"required": [],
"properties": {
"TOM": {
"type": "string"
},
"JACK": {
"type": "string"
},
"LILY": {
"type": "string"
},
"BRUCE": {
"type": "string"
}
}
}
}
}
您需要使用propertyNames
关键字。。。
如果实例是对象,则此关键字验证实例中的属性名称根据提供的架构进行验证。请注意,架构正在测试的属性名称将始终是一串
https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-validation-01#section-6.5.8
{
"$schema": "http://json-schema.org/draft-07/schema",
"properties": {
"NAME":{
"propertyNames": {
"enum": ["TOM", "JACK","LILY","BRUCE"]
}
}
}
}
请在此处查看它的工作情况:https://jsonschema.dev/s/VQa7a