如何在 JSON 架构 (Ruby) 中创建所需的 "patternProperty"
How to make a "patternProperty" required in JSON Schema (Ruby)
考虑以下 JSON:
{
"1234abcd" : {
"model" : "civic"
"made" : "toyota"
"year" : "2014"
}
}
考虑另一个 JSON :
{
"efgh56789" : {
"model" : "civic"
"made" : "toyota"
"year" : "2014"
}
}
如果键是固定的,最外面的字母数字键将有所不同并且是必需的;比方说 "identifier" 然后架构很简单,但是由于键名是可变的,我们必须使用 patternProperties
,我怎样才能想出一个架构来捕获最外层键的这些要求:
- 属性 名称(键)是可变的
- 需要
- 字母数字小写
使用 json-schema : https://github.com/ruby-json-schema/json-schema in ruby.
您可能需要更改正则表达式以适合您的有效键:
{
"patternProperties": {
"^[a-zA-Z0-9]*$":{
"properties": {
"model":{"type":"string"},
"made":{"type":"string"},
"year":{"type":"string"}
}
}
},
"additionalProperties":false
}
当这些属性可变时,您可以做的最好的事情是使用 minProperties
和 maxProperties
。如果你想说你的对象中必须有一个并且只有一个这些字母数字键,你可以使用以下模式。如果你想说必须至少有一个,你可以省略 maxProperties
.
{
"type": "object",
"patternProperties": {
"^[a-z0-9]+$": {
"type": "object",
"properties": {
"model": { "type": "string" },
"make": { "type": "string" },
"year": { "type": "string" }
},
"required": ["model", "make", "year"]
}
},
"additionalProperties": false,
"maxProperties": 1,
"minProperties": 1
}
考虑以下 JSON:
{
"1234abcd" : {
"model" : "civic"
"made" : "toyota"
"year" : "2014"
}
}
考虑另一个 JSON :
{
"efgh56789" : {
"model" : "civic"
"made" : "toyota"
"year" : "2014"
}
}
如果键是固定的,最外面的字母数字键将有所不同并且是必需的;比方说 "identifier" 然后架构很简单,但是由于键名是可变的,我们必须使用 patternProperties
,我怎样才能想出一个架构来捕获最外层键的这些要求:
- 属性 名称(键)是可变的
- 需要
- 字母数字小写
使用 json-schema : https://github.com/ruby-json-schema/json-schema in ruby.
您可能需要更改正则表达式以适合您的有效键:
{
"patternProperties": {
"^[a-zA-Z0-9]*$":{
"properties": {
"model":{"type":"string"},
"made":{"type":"string"},
"year":{"type":"string"}
}
}
},
"additionalProperties":false
}
当这些属性可变时,您可以做的最好的事情是使用 minProperties
和 maxProperties
。如果你想说你的对象中必须有一个并且只有一个这些字母数字键,你可以使用以下模式。如果你想说必须至少有一个,你可以省略 maxProperties
.
{
"type": "object",
"patternProperties": {
"^[a-z0-9]+$": {
"type": "object",
"properties": {
"model": { "type": "string" },
"make": { "type": "string" },
"year": { "type": "string" }
},
"required": ["model", "make", "year"]
}
},
"additionalProperties": false,
"maxProperties": 1,
"minProperties": 1
}