6

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
    }
  }
]
2
  • If name uniqueness isn't important, I'd suggest using the IDs as the object keys instead, and convert the list of objects into an object of objects. An example can be seen here: stackoverflow.com/a/51987954/2868017 Commented Aug 23, 2018 at 14:33
  • The key needs to be unique. Renamed id to value to emphasize that it does not act as an id. Commented Feb 3, 2021 at 9:36

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.