Search code examples
arraysjsonuniquejsonschema

JSON schema deeper object uniqueness


I'm trying to get into JSON schema definitions and wanted to find out, how to achieve a deeper object uniqueness in the schema definition. Please look at the following example definition, in this case a simple IO of a module.

{
    "$schema": "http://json-schema.org/draft-06/schema#",
    "type": "object",
    "required": ["modulIOs"],
    "properties": {
        "modulIOs": {
            "type": "array",
            "uniqueItems": true,
            "items": {
                "allOf": [
                    {
                    "type": "object",
                    "required": ["ioPosition","ioType","ioFunction"],
                    "additionalProperties": false,
                    "properties": {
                        "ioPosition": {
                            "type": "integer"
                        },
                        "ioType": {
                            "type":"string",
                            "enum": ["in","out"]
                        },   
                        "ioFunction": {
                            "type":"string"
                        }
                    }
                }
            ]
        }
        }
    }
}

When I validate the following with i.E. draft-06 I get a positive validation.

{"modulIOs":
    [
          {
            "ioPosition":1,
            "ioType":"in",
            "ioFunction":"240 V AC in"
        },
        {
            "ioPosition":1,
            "ioType":"in",
            "ioFunction":"24 V DC in"
        }
    ]
} 

I'm aware that the validation is successfull because the validator does what he's intended to - it checks the structure of a JSON-object, but is there a possibility to validate object value data in deeper objects or do i need to perform the check elsewhere?


Solution

  • This is not currently possible with JSON Schema (at draft-7).

    There is an issue raised on the official spec repo github for this: https://github.com/json-schema-org/json-schema-spec/issues/538

    If you (or anyone reading this) really wants this, please thumbsup the first issue comment.

    It's currently unlikely to make it into the next draft, and even if it did, time to impleemntations picking it up may be slow.

    You'll need to do this validation after your JSON Schema validation process.