Hirusha Fernando
Hirusha Fernando

Reputation: 1295

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

Upvotes: 1

Views: 542

Answers (1)

Nadika Koshala
Nadika Koshala

Reputation: 68

Try this schema

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

Hope this will be helpful

Upvotes: 3

Related Questions