确保数组中的item属性在Json Schema中是唯一的? [英] Make sure item property in array is unique in Json Schema?

查看:162
本文介绍了确保数组中的item属性在Json Schema中是唯一的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于以下JSON模式,是否有可能表明名称"属性应是唯一的(即,元素"数组中不应有两个具有相同名称"的项目.

Given the following JSON schema, is it possible to indicate that the "name" property shall be unique (i.e. there should NOT be two items with the same "name" in the "elements" array.

{
  "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"
            }
          }
        }
      }
    }
  }
}

我尝试使用 uniqueItems 关键字,但是似乎它是为简单的值列表设计的.

I tried to use the uniqueItems keyword but it seems it was designed for simple lists of values.

推荐答案

否,这是不可能的.从文档中获取json模式:...一种用于定义JSON数据结构的基于JSON的格式.

No, it is not possible. From the docs, json-schema: ...a JSON based format for defining the structure of JSON data.

由于它不是标准的目的,因此在进行数据值验证方面受到很大限制.之前有很多人问过这个问题,因为通常要求一种唯一ID"功能.不幸的是,对于那些需要它的人,json-schema并没有为您提供.

It is quite limited for making data values validation because it is not the purpose of the standard. Many people have asked this before because it is common to request a kind of "unique Id" feature. Unfortunately for those who need it, json-schema does not provides you with that.

因此,如果要确保唯一性,唯一的选择是使用名称"作为属性键而不是属性值.

So if you want to ensure uniqueness your only option is to have "name" as property keys instead of property values.

这篇关于确保数组中的item属性在Json Schema中是唯一的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆