Draft 04 | Draft 06 | Draft 07 | Draft 2019-09 | Draft 2020-12 |
The 3 properties If, Then, Else make it possible to specify conditional validation rules for a schema. Each of them contains a JSON Schema.
The schema described within the If clause is evaluated against the value in the JSON Instance Document.
Example JSON Schema |
Copy Code
|
---|---|
"Value": { "type": [ "integer", "string" ], "if": { "type": "integer" }, "then": { "type": "integer", "maximum": 9999, "minimum": 0 }, "else": { "type": "string", "maxLength": 4, "minLength": 1, "pattern": "\\d+" } }, |
The schema describes a property called "Value" which can contain a string or a Number.
If the value in the instance document is an integer, then the rules in the Then are applied meaning it must be in the range 0-9999.
If the value is not an integer then it must be a string 1-4 chars long matching the regex pattern "\d+".
Example Valid JSON Instance Document Fragments |
Copy Code
|
---|---|
"Value" : "1234" "Value" : 1234 "Value" : "0" |
Example Invalid JSON Instance Document Fragments |
Copy Code
|
---|---|
"Value" : "" "Value" : 100000 "Value" : "FRED" |