|
| 1 | +--- |
| 2 | +title: "Expanding If-Then-Else" |
| 3 | +description: "Learn how to use the if-then-else keyword in JSON Schema to apply conditional validation using more than just constants." |
| 4 | +keywords: "if-then-else, conditional validation, JSON Schema, not, minimum, subschema" |
| 5 | +--- |
| 6 | + |
| 7 | +# Expanding If-Then-Else |
| 8 | +So far, you've learned how to use if-then-else for conditionals. This lesson will show how they can be combined with different validation rules, not just constants! |
| 9 | + |
| 10 | +## Using with not Keyword |
| 11 | +The `not` keyword allows you to negate a condition (or any schema rule), meaning a property must not match a specified type or constraint. For example, the following schema ensures that the name property can be anything except a string: |
| 12 | +```json highlightLineStart={5} highlightLineEnd={5} |
| 13 | +{ |
| 14 | + "type": "object", |
| 15 | + "properties": { |
| 16 | + "name": { |
| 17 | + "type": {"not": {"type": "string"}} |
| 18 | + } |
| 19 | + } |
| 20 | +} |
| 21 | +``` |
| 22 | + |
| 23 | +Using the `not` keyword with the `if-else` keyword can look something like this: |
| 24 | + |
| 25 | +**Example Schema** |
| 26 | +```json highlightLineStart={15} highlightLineEnd={23} |
| 27 | +{ |
| 28 | + "type": "object", |
| 29 | + "properties": { |
| 30 | + "status": { |
| 31 | + "type": "string", |
| 32 | + "enum": ["employed", "unemployed"] |
| 33 | + }, |
| 34 | + "salary": { |
| 35 | + "type": "number" |
| 36 | + }, |
| 37 | + "unemploymentBenefits": { |
| 38 | + "type": "number" |
| 39 | + } |
| 40 | + }, |
| 41 | + "if": { |
| 42 | + "properties": { |
| 43 | + "status": { "const": "employed" } |
| 44 | + } |
| 45 | + }, |
| 46 | + "then": { |
| 47 | + "not": { "required": ["unemploymentBenefits"] |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | +``` |
| 52 | +- If `status` is `"employed"`, the `unemploymentBenefits` field **must not** be present. |
| 53 | +- If `status` is `"unemployed"`, `unemploymentBenefits` field can be present. |
| 54 | + |
| 55 | +This demonstrates that conditionals in JSON Schema can be combined with various keywords, allowing for more flexible validation rules. |
| 56 | + |
| 57 | +## Task |
| 58 | + |
| 59 | +```json |
| 60 | +{ |
| 61 | + "name": "John Doe", |
| 62 | + "age": 20, |
| 63 | + "grade": 8, |
| 64 | + "recommendationLetter": "Dr. Smith's letter", |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +You are given the [schema](https://json-schema.org/learn/glossary#schema) for the same JSON document in the <SideEditorLink/>. Modify the schema to enforce the below condition using `if-then-else`: |
| 69 | + |
| 70 | +- **If** `grade` is **greater that or equal to 8**, **then** the `recommendationLetter` field must be present, and the `personalStatement` field must **NOT** be present. |
| 71 | +- Else if `grade` is **lower than 8**, **then** the `personalStatement` field must be present, and the `recommendationLetter` field must **NOT** be present.. |
| 72 | + |
| 73 | +> **Hint:** Use the `minimum` keyword to specify the constraint of greater than or equal to 8. |
0 commit comments