Search code examples
jsonjsonschemapython-jsonschema

JSON Schema enum with maximum and minimum


I have a JSON object like this.

{
  "test": bla bla bla
}

This test can be a number between 0 and 120 or an empty string. I want to validate this JSON object using a JSON schema like this.

{
  "type": ["number", "string"],
  "enum": [""],
  "minimum": 0,
  "maximum": 120
}

Valid

{"test": ""}
{"test": 0}
{"test": 120}
{"test": 3}

Invalid

{"test": "dfd"}
{"test": -1}
{"test": 675}

What is the correct JSON schema for this? Please help


Solution

  • Try this schema

    {
      "anyOf":[
        {
            "type": "string",
            "enum": [""]
        },
        {
            "type": "number",
            "minimum": 0,
            "maximum": 120
        }
      ]
    }
    

    Hope this will be helpful