Home > Software engineering >  JSON schema validation/unique values
JSON schema validation/unique values

Time:10-15

asking for a bit of guidance ......

I need a JSON schema to validate a document ensuring that the value of "someKey" is unique within a JSON document.

This JSON document shows "someKey" being "key01", "key44", and "key45".

{
  "something01" : "value01",
  "something02" : "value02",
  "something03" : [
                   {
                     "someKey" : "key01",
                     "somethingElse" : "placeholder value"
                   },
                   {
                     "someKey" : "key44",
                     "somethingElse" : "placeholder value"
                   },
                   {
                     "someKey" : "key45",
                     "somethingElse" : "placeholder value"
                   }
                 ]
}

This document shows "someKey" with values of "key01" and "key01" ("key01" is a replicate).

{
  "something01" : "value01",
  "something02" : "value02",
  "something03" : [
                   {
                     "someKey" : "key01",
                     "somethingElse" : "placeholder value"
                   },
                   {
                     "someKey" : "key01",
                     "somethingElse" : "placeholder value"
                   }
                 ]
}

and this document shows "someKey" being "key02", "key05", "key02", and "key02" ("key02" is a replicate)

{
  "something01" : "value01",
  "something02" : "value02",
  "something03" : [
                   {
                     "someKey" : "key02",
                     "somethingElse" : "placeholder value"
                   },
                   {
                     "someKey" : "key05",
                     "somethingElse" : "placeholder value"
                   },
                   {
                     "someKey" : "key02",
                     "somethingElse" : "placeholder value"
                   },
                   {
                     "someKey" : "key02",
                     "somethingElse" : "placeholder value"
                   }
                 ]
}

Basically I need some guidance on how, or whether it is possible, to write a JSON schema that validates for the first document but not the others.

Thanks in advance

CodePudding user response:

You can't express this with standard JSON Schema, but there is a third-party vocabulary that defines a uniqueKeys keyword for this kind of thing. You can find documentation at https://gregsdennis.github.io/json-everything/usage/vocabs-unique-keys.html

{
  "$schema": "https://gregsdennis.github.io/json-everything/meta/unique-keys",
  "type": "object",
  "properties": {
    "something01": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "someKey": { "type": "string" }
        }
      },
      "uniqueKeys": ["/someKey"]
    }
  }
}

As far as I know, JSON Everything (C#) is the only implementation that currently supports this vocabulary. If you use something else, you might need to request that the implementation be updated to support this keyword. Otherwise, you'd need to validate this constraint outside of JSON Schema.

  • Related