Skip to content

Commit

Permalink
docs: add specifying length of an array section
Browse files Browse the repository at this point in the history
  • Loading branch information
JeelRajodiya committed Jun 14, 2024
1 parent 7b084c6 commit 03b281f
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 0 deletions.
103 changes: 103 additions & 0 deletions content/04-Arrays/01-Specifying-Length-of-an-Array/code.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
const code: any = {
type: "object",
properties: {
name: {
type: "string",
},
age: {
type: "integer",
},
},
};

const solution = structuredClone(code);
solution.properties.phones = {
type: "array",
items: {
type: "string",
pattern: "^\\d{3}-\\d{3}-\\d{4}$",
},
minItems: 1,
maxItems: 3,
};

const testCases = [
{
input: {
name: "John Doe",
age: 30,
phones: [],
},
expected: false,
},
{
input: {
name: "John Doe",
age: 30,
phones: ["123-456-7890"],
},
expected: true,
},
{
input: {
name: "John Doe",
age: 30,
phones: ["123-456-78900"],
},
expected: false,
},
{
input: {
name: "John Doe",
age: 30,
phones: ["1a3-456-7g90"],
},
expected: false,
},
{
input: {
name: "John Doe",
age: 30,
phones: ["1234567890"],
},
expected: false,
},
{
input: {
name: "John Doe",
age: 30,
phones: [213],
},
expected: false,
},
{
input: {
name: "John Doe",
age: 30,
phones: ["797-147-7454", "123-456-7890"],
},
expected: true,
},
{
input: {
name: "John Doe",
age: 30,
phones: ["797-147-7454", "123-456-7890", "123-006-7890"],
},
expected: true,
},
{
input: {
name: "John Doe",
age: 30,
phones: ["797-147-7454", "123-456-7890", "123-006-7890", "745-746-5584"],
},
expected: false,
},
];

module.exports = {
code,
solution,
testCases,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
title: Specifying Length of an Array
description: Learn to constrain the number of items in an array using JSON Schema.
keywords: minItems, maxItems, items, validation, JSON, Schema, JSON Schema
---


# Arrays

**Welcome,** to the Arrays module! In this module, you will learn how to use *JSON Schema* to validate arrays, apply constraints on the array items, and handle additional items in an array.

## Specifying Length of an Array

Consider the employee JSON Document, now has a new field: an array of phone numbers.

```json highlightLineStart={4}
{
"name": "John Doe",
"age": 25,
"phones": ["123-456-7890", "987-654-3210"]
}
```


### Schema for the Array

In *JSON Schema*, you can specify the minimum and maximum number of items in an array using the `minItems` and `maxItems` keywords.

**Example**

```
{
"hobbies": {
"type": "array",
"items": {...}
"minItems": 1,
"maxItems": 5
}
}
```
Now, modify the `phones` property of the given schema on the side editor, and specify minimum items as `1` and maximum items as `3`. Additionally constrain the phone numbers to be in the format `xxx-xxx-xxxx`.

> **Hint:** Use regular expression `/^\d{3}-\d{3}-\d{4}$/` with `pattern` keyword to validate the phone number format.
4 changes: 4 additions & 0 deletions content/04-Arrays/index.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
title: Arrays

---

0 comments on commit 03b281f

Please sign in to comment.