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
0c7da19
commit e604abb
Showing
2 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
content/02-Primitive-Types/08-Defining-Constant-Values/code.ts
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,76 @@ | ||
const code: any = { | ||
type: "object", | ||
properties: { | ||
name: { | ||
type: "string", | ||
}, | ||
age: {}, | ||
companyName: {}, | ||
}, | ||
}; | ||
|
||
const solution = structuredClone(code); | ||
solution.properties.companyName = { | ||
const: "Postman", | ||
}; | ||
|
||
solution.properties.age = { | ||
const: 25, | ||
}; | ||
|
||
const testCases: any[] = [ | ||
{ | ||
input: { | ||
name: "person", | ||
age: 25, | ||
companyName: "Postman", | ||
}, | ||
expected: true, | ||
}, | ||
{ | ||
input: { | ||
name: "person", | ||
age: 25, | ||
companyName: "Google", | ||
}, | ||
expected: false, | ||
}, | ||
{ | ||
input: { | ||
name: "person", | ||
age: 26, | ||
companyName: "Postman", | ||
}, | ||
expected: false, | ||
}, | ||
{ | ||
input: { | ||
name: "person", | ||
age: 26, | ||
companyName: "Google", | ||
}, | ||
expected: false, | ||
}, | ||
{ | ||
input: { | ||
name: "person", | ||
age: 24, | ||
companyName: "Postman", | ||
}, | ||
expected: false, | ||
}, | ||
{ | ||
input: { | ||
name: 0, | ||
age: 25, | ||
companyName: "Postman", | ||
}, | ||
expected: false, | ||
}, | ||
]; | ||
|
||
module.exports = { | ||
code, | ||
solution, | ||
testCases, | ||
}; |
43 changes: 43 additions & 0 deletions
43
content/02-Primitive-Types/08-Defining-Constant-Values/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,43 @@ | ||
--- | ||
title: Defining Constant Values | ||
description: Learn to Define constant values in JSON Schema. | ||
keywords: const, constant, JSON Schema | ||
--- | ||
|
||
# Constant Values | ||
|
||
So far, we have used the `enum` keyword to define a list of allowed values for a property. But **what if you want to define a single constant value for a property?** | ||
|
||
Consider, you want to define a property `companyName` with a constant value `Postman`. any other value for `companyName` should be considered invalid. | ||
|
||
```json highlightLineStart={4} | ||
{ | ||
"name":"John Doe", | ||
"age": 25, | ||
"companyName": "Postman" | ||
} | ||
``` | ||
|
||
## Const Keyword | ||
|
||
The `const` keyword is used to define a single constant value for a property. The value of the `const` keyword **can be any valid JSON value**. | ||
|
||
**Example** | ||
|
||
```json | ||
{ | ||
"const": "Postman" | ||
} | ||
``` | ||
|
||
Now, try to add a new property `companyName` in the JSON object such that it should have a constant value `Postman`. | ||
|
||
Just for practice, we will also **set the age to a constant value of 25**. | ||
|
||
|
||
|
||
|
||
<GoodToKnowBox> | ||
- Values defined in `const` are case-sensitive. So `Postman` and `postman` are considered different values. | ||
- You don't need to add `type` keywords when using `const`. The `const` keyword itself implies the type of the value. | ||
</GoodToKnowBox> |