If-Then-Else

if a condition is true, then apply a subschema. else, apply another subschema.

Consider an isFullTime Field. If isFullTime is true, then the salary field must be present, else the hourlyRate field must be present.

1{
2  "name": "John Doe",
3  "isFullTime": true,
4  "salary": 50000
5}

You can use the if-then-else keyword to enforce this condition.

Example Schema

1{
2  "type": "object",
3  "properties": {
4    "isFullTime": {"type": "boolean"},
5    "salary": {"type": "number"},
6    "hourlyRate": {"type": "number"}
7  },
8  "if": {"properties": {"isFullTime": {"const": true}}},
9  "then": {"required": ["salary"]},
10  "else": {"required": ["hourlyRate"] },
11  "required": ["isFullTime"]
12}

Task

1{
2  "name": "John Doe",
3  "isStudent": true,
4  "age": 25
5}

You are given the schema for the same JSON document in the side editor. Modify the schema to enforce the below condition:

  • If isStudent is true, then the age field must be present, else the grade field must be present.
Loading...
Output
Please click the button or use
Shift+Enter
to view the output