Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

json schema validate the value of one property against the value of another property

Let's say I have the follow Json Schema

{
    'type': 'object',
    'properties': {
        'MinimumNumber': {'type':'number'},
        'MaximumNumber': {'type':'number'}
     },
     'required': ['MinimumNumber', 'MaximumNumber'],
     'additionalProperties': false
}

How do I validate that the value of MaximumNumber is higher than the value of MinimumNumber?

invalid object

{
    MinimumNumber: 10,
    MaximumNumber: 5
}

valid object

{
    MinimumNumber: 5,
    MaximumNumber: 10
}
like image 862
Jiren Avatar asked Sep 19 '25 00:09

Jiren


2 Answers

This is a frequently asked question, but no, there is no way in JSON Schema to compare one section of your data against another section. You can do it manually by editing your schema to contain a portion of your data, e.g. via a template.

like image 182
Ether Avatar answered Sep 21 '25 22:09

Ether


@Ether is correct in saying that there is no solution for this with pure JSON Schema. However, there are now vocabularies. I've written one that allows you do just this.

Currently there's only my .Net implementation, but as draft 2020-12 is adopted, this may see more use across implementations in other frameworks.

like image 41
gregsdennis Avatar answered Sep 21 '25 22:09

gregsdennis