Search code examples
jsonjsonschemajson-schema-validatorajv

JSON schema: number greater than (not greater than and equal)


I've the following JSON config:

{
    "zoomLevel": {
        "min": 5,
        "max": 7
    }
}

The ajv validation should check if the max value is larger than the min value and can't be equal. I've found In the docs for the ajv package only the larger than or equal check and I've no idea how should I check for only the larger one. So if I use this schema:

"zoomLevel": {
    "type": "object",
        "properties": {
            "min": {
                "type": "number",
                "minimum": 0
            },
            "max": {
                "type": "number",
                "minimum": {
                    "$data": "1/min"
                },
                "maximum": 18
            }
    }
}

it checks only if the max is larger than or equal to min. I need something like this:

"minimum": {
    "$data": "1/min + 1"
},

Solution

  • The keyword you're looking for is exclusiveMinimum instead of minimum.

    Also, note that $data is non-standard JSON Schema. This is something that ajv have decided to provide and will not be compatible with other validators.