Skip to content

Commit f03581e

Browse files
Added new if-then-else example (Closes #71) (#139)
* Added new if-then-else example (Closes #71) * docs: minor updates * refactor: rename folder * style: format code for consistency and readability --------- Co-authored-by: JeelRajodiya <[email protected]>
1 parent 2a58200 commit f03581e

File tree

2 files changed

+201
-0
lines changed

2 files changed

+201
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
const code: any = {
2+
type: "object",
3+
properties: {
4+
name: {
5+
type: "string",
6+
},
7+
age: {
8+
type: "integer",
9+
},
10+
grade: {
11+
type: "number",
12+
minimum: 0,
13+
maximum: 10,
14+
},
15+
recommendationLetter: {
16+
type: "string",
17+
},
18+
personalStatement: {
19+
type: "string",
20+
},
21+
},
22+
required: ["name", "grade"],
23+
};
24+
25+
let solution = structuredClone(code);
26+
solution = {
27+
...solution,
28+
if: {
29+
properties: {
30+
grade: {
31+
minimum: 8,
32+
},
33+
},
34+
},
35+
then: {
36+
required: ["recommendationLetter"],
37+
not: { required: ["personalStatement"] },
38+
},
39+
else: {
40+
required: ["personalStatement"],
41+
not: { required: ["recommendationLetter"] },
42+
},
43+
};
44+
const testCases = [
45+
{
46+
input: {
47+
name: "John Doe",
48+
age: 20,
49+
grade: 8,
50+
recommendationLetter: "Dr. Smith's letter",
51+
},
52+
expected: true,
53+
},
54+
{
55+
input: {
56+
name: "John Doe",
57+
age: 20,
58+
grade: 5,
59+
personalStatement: "I love engineering..",
60+
},
61+
expected: true,
62+
},
63+
{
64+
input: {
65+
name: "John Doe",
66+
grade: 5,
67+
personalStatement: "",
68+
},
69+
expected: true,
70+
},
71+
{
72+
input: {
73+
name: "John Doe",
74+
age: 20,
75+
grade: 9,
76+
},
77+
expected: false,
78+
},
79+
{
80+
input: {
81+
name: "John Doe",
82+
age: 20,
83+
grade: 5,
84+
},
85+
expected: false,
86+
},
87+
{
88+
input: {
89+
name: "John Doe",
90+
age: 20,
91+
grade: 5,
92+
recommendationLetter: "",
93+
personalStatement: "",
94+
},
95+
expected: false,
96+
},
97+
{
98+
input: {
99+
name: "John Doe",
100+
age: 20,
101+
grade: 8,
102+
recommendationLetter: "",
103+
personalStatement: "",
104+
},
105+
expected: false,
106+
},
107+
{
108+
input: {
109+
name: "John Doe",
110+
age: 20,
111+
recommendationLetter: "",
112+
},
113+
expected: false,
114+
},
115+
{
116+
input: {
117+
age: 20,
118+
grade: 8,
119+
},
120+
expected: false,
121+
},
122+
];
123+
124+
module.exports = {
125+
code,
126+
solution,
127+
testCases,
128+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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

Comments
 (0)