Blog JSON Schema

Validating JSON Schema: All You Need to Know

April 12, 2023
JSON Schema

The sanctity of data is one of the most important things to test when testing APIs, especially when dealing with downstream systems. Testing your API response against the JSON Schema standard is one way to achieve this. A robust tool for validating JSON data is JSON Schema. 

Developers can make sure that the data they receive or create complies with a set of guidelines and limitations because it provides a standard format for defining the structure and content of JSON data. The use of validation keywords to specify these guidelines and drawbacks is one of the main characteristics of JSON Schema.

Required Keyword

Which properties in an object must exist is specified using the required keyword. Each string in the array accepts the name of a necessary property. A required property must be present for an object to pass validation. For illustration, the following schema demands that an object have the name and age properties:

{

  “type”: “object”,

  “required”: [“name”, “age”],

  “properties”: {

    “name”: { “type”: “string” },

    “age”: { “type”: “integer” }

  }

}

Type Keyword

The type of a JSON value is specified using the type keyword. It can accept a single string or an array of strings, each of which is a legitimate JSON type. Validation fails if a value does not fit the designated type. The following schema, for instance, demands that a value be a string:

{

  “type”: “string”

}

Enum Keyword

The ‘enum’ keyword specifies a list of potential values for a property. Each value in the JSON array must match a valid value for the property. If a value does not match any predefined values, it is not considered valid. A value must fall under one of the three categories in the following schema, for instance:

{

  “type”: “string”,

  “enum”: [“red”, “green”, “blue”]

}

Minimum and Maximum Keywords

The minimum and maximum values assigned to a property are specified using the minimum and maximum keywords. They ask for a number that represents the lowest or highest value permitted. The value fails validation if it falls below the minimum or exceeds the maximum. For instance, the following schema demands that a value fall within the range of 18 and 100:

{

  “type”: “integer”,

  “minimum”: 18,

  “maximum”: 100

}

exclusiveMinimum and exclusiveMaximum Keywords

To remove the minimum and maximum values from the permitted range, use the exclusiveMinimum and exclusiveMaximum keywords. The following schema, for instance, stipulates that a value must be greater than 0 and less than 100.

{

  “type”: “number”,

  “exclusiveMinimum”: 0,

  “exclusiveMaximum”: 100

}

Multipleof Keyword

Specify a multiple that a numeric value must be divisible using the multipleOf keyword. It requires a multiple of a number. A value fails validation if it cannot be divided by the required multiple.

The following schema, for instance, demands that a value be a multiple of 5:

{

 “type”: “integer”,

  “multipleOf”: 5

}

Validation Keywords for Strings Pattern Keyword

When a string value must match a regular expression pattern, the pattern keyword specifies that requirement. The pattern for the regular expression is a string that is required. A string fails validation if it does not match the pattern.

The following schema, for instance, demands that a value be a string that begins with “https://”:

{

  “type”: “string”,

  “pattern”: “^https://”

}

Length Keywords

The minimum and maximum lengths of string can be specified using the minLength and maxLength keywords, respectively. Request a non-negative integer that represents either the smallest or largest permitted length. A string fails validation if it is either shorter than the minLength or longer than the maxLength.

The following schema, for instance, mandates that a value be a string with between 5 and 10 characters:

{

  “type”: “string”,

  “minLength”: 5,

  “maxLength”: 10

}

Format Keywords

A string’s required format can be specified using the format keyword. The format is a string that is required. Typical formats include:

“date”: A string of dates with the YYYY-MM-DD format.

‘time’: A string of dates in the format HH:MM:SS.

“datetime”: A string of dates and times in the YYYY-MM-DDT format.HH:MM:SSZ.

“email”: A list of email addresses.

A string fails validation if the format is not followed exactly.

As an illustration, the following schema demands that a value be a string that represents the authentic email address:

{

  “type”: “string”,

  “format”: “email”

}

Validation Keywords for Arrays

Arrays are a frequent data type in JSON, and validation is frequently needed to make sure they abide by established guidelines and restrictions. For arrays, JSON Schema offers a number of validation keywords that use to impose these restrictions. Look at a few of the most popular JSON Schema array validation keywords.

Items Keyword

If you want to specify a validation schema for every item in an array, you can use the “items” keyword. This keyword accepts either a single schema or a collection of schemas. If a single schema provides, all items in the array are subject to it. On the other hand, if an array of schemas is provided, each schema is applied to the corresponding item in the array.

For example, the following schema specifies that a value must be an array of integers:

{

  “type”: “array”,

  “items”: {

    “type”: “integer”

  }

}

Alternately, the following schema mandates that a value be an array with an integer as the first item and a string as the second item.

{

  “type”: “array”,

  “items”: [

    {“type”: “integer”},

    {“type”: “string”}

  ]

}

Additional Items Keyword

The validation schema for additional items in an array is specified using the additionalItems keyword. When there are additional items in the array for which there is no corresponding schema, and the “items” keyword is an array of schemas. All additional items in the array are subject to the schema specified by the additionalItems keyword.

For instance, the following schema mandates that a value be an array with an integer as the first item and a string as the second item. You can add any type of additional items to the array:

{

  “type”: “array”,

  “items”: [

    {“type”: “integer”},

    {“type”: “string”}

  ],

  “additionalItems”: true

}

Min and Max Items Keywords

The minimum and maximum number of items in an array is specified, using the minItems and maxItems keywords. The minimum or maximum number of items they accept is a non-negative integer. An array fails validation if the number of items is less than the minItems or greater than the maxItems.

For instance, the following schema demands that a value be an array with a minimum of 3 and a maximum of 5 elements:

{

  “type”: “array”,

  “minItems”: 3,

  “maxItems”: 5

}

Unique Items Keyword

To specify whether each item in an array must be unique, use the uniqueItems keyword. It accepts a boolean result. If true, each element of the array needs to be distinct. Items repeat if set to false or excluded.

For illustration, the following schema mandates that a value be an array of distinct items:

{

  “type”: “array”,

  “uniqueItems”: true

}

Validation Keywords for Objects Properties Keyword

For each property of an object, a schema using the properties keyword. It requires the object whose keys are the names of the properties and whose values are schemas outlining those properties.

The following schema, for instance, mandates that a value be an object with the properties name and age. Both the name and age properties must be strings, respectively.

{

  “type”: “object”,

  “properties”: {

    “name”: {“type”: “string”},

    “age”: {“type”: “integer”}

  }

}

Additional Properties Keyword

The validation schema for additional properties in an object is specified using the additionalProperties keyword. It is used when the properties keyword does not specify a schema for every other object’s properties. All additional properties in the object will have the schema specified by the additionalProperties keyword applied to them.

The following schema, for instance, mandates that a value be an object with the properties name and age. Any type of additional property is possible.

{

  “type”: “object”,

  “properties”: {

    “name”: {“type”: “string”},

    “age”: {“type”: “integer”}

  },

  “additionalProperties”: true

}

Min and Max Properties Keywords

The minimum and maximum number of properties in an object is specified particularly by the minProperties and maxProperties keywords. The minimum or maximum number of properties they can accept is a non-negative integer. An object fails validation if it has fewer properties than the minProperties or more properties than the maxProperties.

For illustration, the following schema demands that a value be an object with a minimum of two properties and a maximum of five properties:

{

  “type”: “object”,

  “minProperties”: 2,

  “maxProperties”: 5

}

Dependencies Keyword

Dependencies between object properties are specified using the dependencies keyword. It requires an object with schemas or arrays of property names as values and property names as keys. The corresponding schema or array of property names must be present if the object’s key property is present.

JSON data must adhere to a number of rules and restrictions, and validation keywords are an effective tool for doing this.

In this article, we have gone through some of the most popular JSON Schema validation keywords, such as required, type, enum, minimum and maximum, and pattern. By including these keywords in your JSON schemas, you can ensure that your JSON data is consistently valid and complies with your particular requirements. 

Leave a Reply