Primitive Types

Welcome to the Primitive Types module. In this module, you will learn about all the primitive types available in JSON Schema and some common constraints that can be applied to them. Let's start with the string type.

Constraining String Length

Let's add two new fields postalCode and phoneNumber to the JSON object. Now, How do you ensure that those fields have the correct length?

1{
2  "name": "John Doe",
3  "age": 25,
4  "postalCode": "385004",
5  "phoneNumber": "84584898564"
6}

minLength and maxLength

In JSON Schema, you can use the minLength and maxLength keywords to define the minimum and maximum length of a string.

Example

1{
2  ...
3  "postalCode": {
4    "type": "string",
5    "minLength": 6,
6    "maxLength": 6
7  }
8}

Now, try to modify the postalCode and phoneNumber fields in the side editor with the below constraints in mind.

Constraints:

  • The postalCode should be a string with a length of exactly 6 characters.
  • The phoneNumber should be a string with a minimum length of 10 characters.
Loading...
Output
Please click the button or use
Shift+Enter
to view the output