鉴于以下JSON模式是有可能表明,"名称"属性应是唯一的(即不应该有两个项目具有相同的"名"的"元素"阵列英寸
{
"root": {
"type": "object",
"properties": {
"elements": {
"type": "array",
"minItems": 1,
"items": {
"type": "object",
"properties": {
"name": {
"type": "string",
"title": "Element Name",
"minLength": 3,
},
"url": {
"type": "string",
"title": "Some URL"
}
}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用uniqueItems关键字,但它似乎是为简单的值列表而设计的.
jru*_*ren 23
不,这是不可能的.从文档中,json-schema:...一种基于JSON的格式,用于定义JSON数据的结构.
它对于进行数据值验证非常有限,因为它不是标准的目的.许多人之前已经问过这个问题,因为通常会要求一种"唯一ID"功能.不幸的是,对于那些需要它的人来说,json-schema并没有为你提供.
因此,如果您想确保唯一性,您唯一的选择是将"name"作为属性键而不是属性值.
如果重构数据结构是一个选项,以下方法可能会有所帮助:
patternProperties.模式是正则表达式.匹配模式的任何对象都将根据pattern-property的模式进行验证.匹配任何字符串> = 3个字符的模式如下所示:"....*"但似乎".*"始终隐含尾随,因此"..."也可以. minLength:3)的附加步骤.minItems:1用于数组),请替换minItems为minProperties....导致下面的架构:
"root": {
"type": "object",
"properties": {
"elements": {
"type": "object",
"patternProperties": {
"...": {
"type": "object",
"properties": {
"url": {
"type": "string"
}
}
}
},
"additionalProperties": false,
"minProperties": 1
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果以下文档(摘录)与您的旧架构匹配,
"elements": [
{
"name": "abc",
"url": "http://myurl1"
},
{
"name": "def",
"url": "http://myurl2"
},
{
"name": "ghij",
"url": "http://myurlx"
}
]
Run Code Online (Sandbox Code Playgroud)
...这样的文档(摘录)将匹配新架构:
"elements": {
"abc": {
"url": "http://myurl1"
},
"def": {
"url": "http://myurl2"
},
"ghij": {
"url": "http://myurlx"
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
对于 Ajv 验证器,您可以使用自定义 JSON-Schema 关键字uniqueItemProperties: ajv-validator/ajv-keywords
| 归档时间: |
|
| 查看次数: |
11162 次 |
| 最近记录: |