Skip to content

Commit

Permalink
docs: array mincontains and maxcontains
Browse files Browse the repository at this point in the history
  • Loading branch information
JeelRajodiya committed Jun 17, 2024
1 parent d4b2b90 commit 270533e
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 0 deletions.
84 changes: 84 additions & 0 deletions content/04-Arrays/07-minContains-and-maxContains/code.ts
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 content/04-Arrays/07-minContains-and-maxContains/instructions.mdx
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.


0 comments on commit 270533e

Please sign in to comment.