Json schema validation: Unique key in json array

Viewed 4810

I have following JSON data:

[
  {
  "unique1":{
    "value":3
    }
  },
  {
  "unique2":{
    "value":4
    }
  }
]

Each array item has a json object with one top level unique key. When i try to write a validation schema for it I can only validate that the full array is unique but not the top level key in each array.

{
  "$schema": "http://json-schema.org/draft-06/schema#",
  "type": "array",
  "uniqueItems": true,
  "items": {
    "type": "object",
    "patternProperties": {
      "^.*$": {
      }
    }
  }
}

Following JSON data should fail to validate:

[
  {
  "unique1":{
    "value":3
    }
  },
  {
  "unique1":{
    "value":4
    }
  }
]
1 Answers

There is no standard JSON Schema keyword that allows to express this validation requirement.

Ajv (for JavaScript) has a custom keyword "uniqueItemProperties" (in ajv-keywords package) that does what you ask for.

You may propose it for the next versions of the standard.

Related