generated from json-schema-org/repo-template
-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: constraining number of properties in objects
- Loading branch information
1 parent
b3ba1fc
commit 752c1c3
Showing
2 changed files
with
170 additions
and
0 deletions.
There are no files selected for viewing
126 changes: 126 additions & 0 deletions
126
content/03-Objects/03-Constraining-Number-of-Properties/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,126 @@ | ||
const code: any = { | ||
type: "object", | ||
properties: { | ||
name: { | ||
type: "string", | ||
}, | ||
age: { | ||
type: "integer", | ||
}, | ||
contactMethods: { | ||
type: "object", | ||
}, | ||
}, | ||
}; | ||
|
||
const solution = structuredClone(code); | ||
solution.properties.contactMethods = { | ||
additionalProperties: { | ||
type: "string", | ||
}, | ||
minProperties: 2, | ||
maxProperties: 5, | ||
}; | ||
|
||
const testCases = [ | ||
{ | ||
input: { | ||
name: "John Doe", | ||
age: 30, | ||
contactMethods: { | ||
email: "[email protected]", | ||
phone: "1234567890", | ||
phone2: "1234567890", | ||
}, | ||
}, | ||
expected: true, | ||
}, | ||
{ | ||
input: { | ||
name: "John Doe", | ||
age: 30, | ||
contactMethods: { | ||
email: "[email protected]", | ||
phone: "1234567890", | ||
}, | ||
}, | ||
expected: true, | ||
}, | ||
{ | ||
input: { | ||
name: "John Doe", | ||
age: 30, | ||
contactMethods: {}, | ||
}, | ||
expected: false, | ||
}, | ||
{ | ||
input: { | ||
name: "John Doe", | ||
age: 30, | ||
contactMethods: { | ||
email: "[email protected]", | ||
}, | ||
}, | ||
expected: false, | ||
}, | ||
{ | ||
input: { | ||
name: "John Doe", | ||
age: 30, | ||
contactMethods: { | ||
email: "[email protected]", | ||
phone: "1234567890", | ||
phone2: "1234567890", | ||
email2: "[email protected]", | ||
}, | ||
}, | ||
expected: true, | ||
}, | ||
{ | ||
input: { | ||
name: "John Doe", | ||
age: 30, | ||
contactMethods: { | ||
email: 0, | ||
phone: null, | ||
}, | ||
}, | ||
expected: false, | ||
}, | ||
{ | ||
input: { | ||
name: "John Doe", | ||
age: 30, | ||
contactMethods: { | ||
email: "[email protected]", | ||
phone: "1234567890", | ||
phone2: "1234567890", | ||
email2: "[email protected]", | ||
phone3: "1234567890", | ||
}, | ||
}, | ||
expected: true, | ||
}, | ||
{ | ||
input: { | ||
name: "John Doe", | ||
age: 30, | ||
contactMethods: { | ||
email: "[email protected]", | ||
phone: "1234567890", | ||
phone2: "1234567890", | ||
email2: "[email protected]", | ||
phone3: "1234567890", | ||
fax: "1234567890", | ||
}, | ||
}, | ||
expected: false, | ||
}, | ||
]; | ||
|
||
module.exports = { | ||
code, | ||
solution, | ||
testCases, | ||
}; |
44 changes: 44 additions & 0 deletions
44
content/03-Objects/03-Constraining-Number-of-Properties/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,44 @@ | ||
--- | ||
title: Constraining Number of Properties | ||
description: Learn how to set the minimum and maximum number of properties in an object in JSON Schema. | ||
keywords: minProperties, maxProperties, object, JSON Schema, properties, type, properties, required, properties, object | ||
--- | ||
|
||
|
||
# Constraining Number of Properties | ||
|
||
In JSON Schema, you can set constraints on the number of properties an object can have using the `minProperties` and `maxProperties` keywords. | ||
|
||
- `minProperties` specifies the **minimum number of properties an object must have.** | ||
- `maxProperties` specifies the **maximum number of properties an object can have.** | ||
|
||
Let's add a new property `contactMethods` in the employee object and set constraints on the number of properties. | ||
|
||
```json highlightLineStart={4} highlightLineEnd={7} | ||
{ | ||
"name": "John Doe", | ||
"age": 25, | ||
"contactMethods": { | ||
"email": "[email protected]", | ||
"phone": "1234567890", | ||
"mobile": "0987654321" | ||
} | ||
} | ||
``` | ||
|
||
Now, add the `minProperties` and `maxProperties` keywords to the schema on the right side editor to set the minimum and maximum number of properties in the `contactMethods` object. | ||
|
||
In the `contactMethods` object, set the minimum number of properties to `2` and the maximum number of properties to `5`. | ||
|
||
|
||
**Example** | ||
|
||
```json | ||
{ | ||
"type": "object", | ||
"minProperties": 2, | ||
"maxProperties": 5 | ||
} | ||
``` | ||
|
||
Additionally, in the `contactMethods` object, use the `additionalProperties` keyword to ensure that the values of the properties are `string` type. |