1

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

Hirusha Fernando
  • 1,156
  • 10
  • 29

1 Answers1

3

Try this schema

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

Hope this will be helpful