Make sure item property in array is unique in Json Schema?

begie picture begie · Jul 15, 2014 · Viewed 15.9k times · Source

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

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

Answer

jruizaranguren picture jruizaranguren · Jul 28, 2014

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

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.