Skip to content

Commit

Permalink
docs: add implication
Browse files Browse the repository at this point in the history
  • Loading branch information
JeelRajodiya committed Jul 15, 2024
1 parent 52485e3 commit 2ef7fe9
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 0 deletions.
78 changes: 78 additions & 0 deletions content/05-Conditional-Validation/05-implications/code.ts
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 content/05-Conditional-Validation/05-implications/instructions.mdx
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.

0 comments on commit 2ef7fe9

Please sign in to comment.