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: add specifying length of an array section
- Loading branch information
1 parent
7b084c6
commit 03b281f
Showing
3 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
103 changes: 103 additions & 0 deletions
103
content/04-Arrays/01-Specifying-Length-of-an-Array/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,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, | ||
}; |
43 changes: 43 additions & 0 deletions
43
content/04-Arrays/01-Specifying-Length-of-an-Array/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: 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. |
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,4 @@ | ||
--- | ||
title: Arrays | ||
|
||
--- |