Skip to content

Commit

Permalink
docs: constraining number of properties in objects
Browse files Browse the repository at this point in the history
  • Loading branch information
JeelRajodiya committed Jun 10, 2024
1 parent b3ba1fc commit 752c1c3
Show file tree
Hide file tree
Showing 2 changed files with 170 additions and 0 deletions.
126 changes: 126 additions & 0 deletions content/03-Objects/03-Constraining-Number-of-Properties/code.ts
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,
};
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.

0 comments on commit 752c1c3

Please sign in to comment.