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.
docs: array mincontains and maxcontains
- Loading branch information
1 parent
d4b2b90
commit 270533e
Showing
2 changed files
with
129 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,84 @@ | ||
const code: any = { | ||
type: "object", | ||
properties: { | ||
name: { | ||
type: "string", | ||
}, | ||
age: { | ||
type: "integer", | ||
}, | ||
workedHours: { | ||
type: "array", | ||
}, | ||
}, | ||
}; | ||
|
||
const solution = structuredClone(code); | ||
|
||
solution.properties.workedHours = { | ||
...solution.properties.workedHours, | ||
contains: { | ||
type: "integer", | ||
minimum: 8, | ||
maximum: 12, | ||
}, | ||
minContains: 2, | ||
}; | ||
|
||
const testCases = [ | ||
{ | ||
input: { | ||
name: "John Doe", | ||
age: 30, | ||
workedHours: [8, 9, 10, 11, 12], | ||
}, | ||
expected: true, | ||
}, | ||
{ | ||
input: { | ||
name: "John Doe", | ||
age: 30, | ||
workedHours: [8, 9], | ||
}, | ||
expected: true, | ||
}, | ||
{ | ||
input: { | ||
name: "John Doe", | ||
age: 30, | ||
workedHours: [8, 12], | ||
}, | ||
expected: true, | ||
}, | ||
{ | ||
input: { | ||
name: "John Doe", | ||
age: 30, | ||
workedHours: [7, 13], | ||
}, | ||
expected: false, | ||
}, | ||
{ | ||
input: { | ||
name: "John Doe", | ||
age: 30, | ||
workedHours: [8, 13], | ||
}, | ||
expected: false, | ||
}, | ||
|
||
{ | ||
input: { | ||
name: "John Doe", | ||
age: 30, | ||
workedHours: [7, 12], | ||
}, | ||
expected: false, | ||
}, | ||
]; | ||
|
||
module.exports = { | ||
code, | ||
solution, | ||
testCases, | ||
}; |
45 changes: 45 additions & 0 deletions
45
content/04-Arrays/07-minContains-and-maxContains/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,45 @@ | ||
--- | ||
title: minContains and maxContains | ||
description: Learn how to use the 'minContains' and 'maxContains' keywords in JSON Schema to ensure that arrays contain a specific number of elements. | ||
keywords: array, minContains, maxContains, validation, JSON Schema | ||
--- | ||
|
||
|
||
# minContains and maxContains | ||
|
||
Previously, we learned how to use the `contains` keyword to ensure that an array contains **at least one element** that matches a specific *subschema*. | ||
|
||
However, there are scenarios where we need to ensure that an array contains a specific number of elements that match a *subschema*. | ||
|
||
You can use the `minContains` and `maxContains` keywords along with `contains` to specify the minimum and maximum number of elements that an array must contain to be valid. | ||
|
||
## Example | ||
|
||
The following example demonstrates how to use the `minContains` and `maxContains` keywords to ensure that an array contains exactly 2 elements that are less than 10. | ||
|
||
```json | ||
{ | ||
"type": "array", | ||
"contains": { | ||
"type": "number", | ||
"maximum": 10 | ||
}, | ||
"minContains": 2, | ||
"maxContains": 2 | ||
} | ||
``` | ||
|
||
## Task | ||
|
||
|
||
```json highlightLineStart={4} | ||
{ | ||
"name": "John Doe", | ||
"age": 30, | ||
"workedHours": [9, 10, 8, 11] | ||
} | ||
``` | ||
|
||
Our employee has field called worked hours. We want to ensure that the `workedHours` array contains **at least 2 elements** that are **greater than or equal to 8 and less than or equal 12**. Modify the schema given to you in the side editor to apply this constraint. | ||
|
||
|