Eas*_*ons 3 ruby validation json jsonschema
考虑以下JSON:
{
"1234abcd" : {
"model" : "civic"
"made" : "toyota"
"year" : "2014"
}
}
Run Code Online (Sandbox Code Playgroud)
考虑另一个JSON:
{
"efgh56789" : {
"model" : "civic"
"made" : "toyota"
"year" : "2014"
}
}
Run Code Online (Sandbox Code Playgroud)
如果密钥是固定的,最外面的字母数字键将变化并且是必需的; 让我们说"标识符"然后架构是直截了当的,但是由于键名是可变的,我们必须使用patternProperties,我如何能够提出一个架构来捕获最外层键的这些要求:
使用json-schema:https://github.com/ruby-json-schema/json-schema in ruby.
Jas*_*ers 13
当这些属性是可变的时,您可以做的最好的事情就是使用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
}
Run Code Online (Sandbox Code Playgroud)
您可能必须更改正则表达式以适合您的有效密钥:
{
"patternProperties": {
"^[a-zA-Z0-9]*$":{
"properties": {
"model":{"type":"string"},
"made":{"type":"string"},
"year":{"type":"string"}
}
}
},
"additionalProperties":false
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8458 次 |
| 最近记录: |