generated from json-schema-org/repo-template
-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
52485e3
commit 2ef7fe9
Showing
2 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
const code: any = { | ||
type: "object", | ||
properties: { | ||
name: { | ||
type: "string", | ||
}, | ||
age: { | ||
type: "integer", | ||
}, | ||
isStudent: { | ||
type: "boolean", | ||
}, | ||
}, | ||
|
||
required: ["name"], | ||
}; | ||
|
||
const solution = structuredClone(code); | ||
solution = { | ||
...solution, | ||
if: { | ||
properties: { | ||
isStudent: { | ||
const: true, | ||
}, | ||
}, | ||
}, | ||
then: { | ||
required: ["age"], | ||
}, | ||
}; | ||
const testCases = [ | ||
{ | ||
input: { | ||
name: "John Doe", | ||
isStudent: true, | ||
age: 20, | ||
}, | ||
expected: true, | ||
}, | ||
{ | ||
input: { | ||
name: "John Doe", | ||
isStudent: true, | ||
grade: 8, | ||
}, | ||
expected: false, | ||
}, | ||
{ | ||
input: { | ||
name: "John Doe", | ||
isStudent: false, | ||
}, | ||
expected: true, | ||
}, | ||
{ | ||
input: { | ||
name: "John Doe", | ||
isStudent: true, | ||
grade: 4, | ||
}, | ||
expected: false, | ||
}, | ||
{ | ||
input: { | ||
name: "John Doe", | ||
isStudent: false, | ||
age: 40, | ||
}, | ||
expected: true, | ||
}, | ||
]; | ||
|
||
module.exports = { | ||
code, | ||
solution, | ||
testCases, | ||
}; |
50 changes: 50 additions & 0 deletions
50
content/05-Conditional-Validation/05-implications/instructions.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
--- | ||
title: Implication | ||
description: Learn how to use the `if-then` keyword to enforce Implication in JSON Schema. | ||
keywords: If-Then, Implication, conditional validation, JSON Schema | ||
--- | ||
|
||
|
||
# Implication using if-then keyword | ||
|
||
**Implication** is a logical operation that states that **if** condition `A` is true, **then** condition `B` must also be true. or in other words **A -> B** (pronounced, A implies B) | ||
|
||
You can use the `if-then` keyword to enforce this condition. | ||
|
||
Consider an `isFullTime` Field. **If** `isFullTime` is true, **then** the `salary` field must be present. | ||
|
||
|
||
```json highlightLineStart={3} highlightLineEnd={4} | ||
{ | ||
"name": "John Doe", | ||
"isFullTime": true, | ||
"salary": 50000 | ||
} | ||
``` | ||
You can use the `if-then` keyword to enforce this condition. | ||
|
||
**Example Schema** | ||
|
||
```json highlightLineStart={8} highlightLineEnd={10} | ||
{ | ||
"type": "object", | ||
"properties": {...}, | ||
"if": {"properties": {"isFullTime": {"const": true}}}, | ||
"then": {"required": ["salary"]}, | ||
} | ||
``` | ||
|
||
## Task | ||
|
||
|
||
```json | ||
{ | ||
"name": "John Doe", | ||
"isStudent": true, | ||
"age": 25 | ||
} | ||
``` | ||
|
||
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. |