Search code examples
jsonjsonschema

Ensure one value is less than another with JSON schema


Imagine I have a following json schema:

"period": {
           "type": "object",
           "properties": {
           "start": {
              "description": "Unixtime",
              "type": "number",
              "minimum": 0
            },
           "end": {
              "description": "Unixtime",
              "type": "number",
              "minimum": 0
            }
          },
          "required": [
           "start",
           "end"
          ]
        }

Is there a way to ensure that start time is always <= end time with help of schema itself?


Solution

  • JSON Schema on its own doesn't support this, but if you're in .net, my data extension vocabulary can do it.

    See my docs for an example: https://docs.json-everything.net/schema/examples/data-ref/#example-schemadata-comparative

    To support dates, you'll also need to define keywords that understand string-encoded dates, like minDate and maxDate.

    These two combined will get you where you need to be.