5

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?

iLuvLogix
  • 5,920
  • 3
  • 26
  • 43

2 Answers2

6

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.

Relequestual
  • 11,631
  • 6
  • 47
  • 83
1

You can validate data value of your object fields by using JSON schema validation. For example, if you need to check if ioPosition is between 0 and 100 you can use:

"ioPosition": {
            "type": "integer",
            "minimum": 0,
            "maximum": 100
          }

If you need to validate ioFunction field you can use regualr expression such as:

 "ioFunction": {
            "type": "string",
            "pattern": "^[0-9]+ V [A,D]C"
          }

Take a look at json-schema-validation.

baymax
  • 151
  • 3
  • Thanks for the hint on that ;) But regarding uniqueness this won't help in that case -> in my example the 'ioPosition'.. – iLuvLogix Jun 01 '18 at 11:22
  • Yes this is not possible since uniqueness is limited to simple array and "ioPosition" is a field of a user defined complex object as explained here https://stackoverflow.com/questions/24763759/how-to-if-possible-define-in-json-schema-one-of-array-items-property-shall-be?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – baymax Jun 01 '18 at 14:29