Jiren
Jiren

Reputation: 673

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
}

Upvotes: 4

Views: 1550

Answers (2)

gregsdennis
gregsdennis

Reputation: 8428

@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.

Upvotes: 1

Ether
Ether

Reputation: 53996

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.

Upvotes: 1

Related Questions