FatalCatharsis
FatalCatharsis

Reputation: 3567

Different minimum and maximum for integer and number in json schema

Let's say I have schema where a property can be either an integer or a number. Is there a way to specify a different maximum depending on the type of value? For example:

{
  "type": ["integer", "number"],
  "max-if-integer": 255,
  "max-if-number": 1.0
}

couldn't find anything about it in the docs.

Upvotes: 1

Views: 5934

Answers (2)

Ethan
Ethan

Reputation: 725

if you are on draft7 or later, if/then/else is probably the best way to express this. if it's earlier, you can get there with oneOf, as discussed on gregsdennis's answer.

{
  "if": {"type": "integer"},
  "then": {
    "minimum": integer-minimum,
    "maximum": integer-maximum
  },
  "else": {
    "type": "number",
    "minimum": float-minimum,
    "maximum": float-maximum
  }
}

do note however that 1.0 and 1 are both considered integers according to the json schema spec, and will use the integer then, not the number else like non-whole-number floats.

Upvotes: 4

gregsdennis
gregsdennis

Reputation: 8428

oneOf is going to be your friend!

{
  "oneOf": [
    { "type": "integer", "maximum": 255 },
    { "type": "number", "maximum": 1 }
  ]
}

Upvotes: 1

Related Questions