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.

1{
2  "name": "John Doe",
3  "age": 25,
4  "contactMethods": {
5    "email": "[email protected]",
6    "phone": "1234567890",
7    "mobile": "0987654321"
8  }
9}

Now, add the minProperties and maxProperties keywords to the schema on the 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

1{
2  "type": "object",
3  "minProperties": 2,
4  "maxProperties": 5
5}

Additionally, in the contactMethods object, use the additionalProperties keyword to ensure that the values of the properties are string type.

Loading...
Output
Please click the button or use
Shift+Enter
to view the output