When testing API’s, one of the key things to test is the sanctity of data, particularly in the case of downstream systems. One of the ways to do that is have your API response test against a set schema, called JSON Schema.
JSON Schema is a powerful tool for validating JSON data. It provides a standardized format for defining the structure and content of JSON data, which allows developers to ensure that data they receive or generate conforms to certain rules and constraints. One of the key features of JSON Schema is the use of validation keywords, which are used to define these rules and constraints.
In this blog post, we’ll explore the most common validation keywords in JSON Schema validation and how they can be used to validate JSON data
Validation Keywords for Any Instance Types
Required Keyword
The required
keyword is used to specify which properties are required in an object. It takes an array of strings, where each string is the name of a required property. If an object does not contain a required property, it fails validation.
For example, the following schema specifies that an object must have a name
property and a age
property:
{
"type": "object",
"required": ["name", "age"],
"properties": {
"name": { "type": "string" },
"age": { "type": "integer" }
}
}
Type Keyword
The type
keyword is used to specify the type of a JSON value. It can take a string or an array of strings, where each string is a valid JSON type. If a value does not match the specified type, it fails validation.
For example, the following schema specifies that a value must be a string:
{
"type": "string"
}
Enum Keyword
The enum
keyword is used to specify a list of possible values for a property. It takes an array of JSON values, where each value is a valid value for the property. If a value does not match any of the specified values, it fails validation.
For example, the following schema specifies that a value must be either “red”, “green”, or “blue”:
{
"type": "string",
"enum": ["red", "green", "blue"]
}
Validation Keywords for Numeric Types (integer and Number)
Minimum and Maximum Keywords
The minimum
and maximum
keywords are used to specify the minimum and maximum allowed values for a property. They take a number, which is the minimum or maximum allowed value. If a value is less than the minimum or greater than the maximum, it fails validation.
For example, the following schema specifies that a value must be between 18 and 100:
{
"type": "integer",
"minimum": 18,
"maximum": 100
}
exclusiveMinimum and exclusiveMaximum Keywords
The exclusiveMinimum
and exclusiveMaximum
keywords can be used to exclude the minimum and maximum values from the allowed range. For example, the following schema specifies that a value must be greater than 0 and less than 100:
{
"type": "number",
"exclusiveMinimum": 0,
"exclusiveMaximum": 100
}
Multipleof Keyword
The multipleOf
keyword is used to specify a multiple that a numeric value must be divisible by. It takes a number, which is the multiple. If a value is not divisible by the specified multiple, it fails validation.
For example, the following schema specifies that a value must be a multiple of 5:
{
"type": "integer",
"multipleOf": 5
}
Validation Keywords for Strings
Pattern Keyword
The pattern
keyword is used to specify a regular expression pattern that a string value must match. It takes a string, which is the regular expression pattern. If a string does not match the pattern, it fails validation.
For example, the following schema specifies that a value must be a string that starts with “https://”:
{
"type": "string",
"pattern": "^https://"
}
Length Keywords
The minLength
and maxLength
keywords are used to specify the minimum and maximum length of a string, respectively. They take a non-negative integer, which is the minimum or maximum allowed length. If a string is shorter than the minLength
or longer than the maxLength
, it fails validation.
For example, the following schema specifies that a value must be a string between 5 and 10 characters long:
{
"type": "string",
"minLength": 5,
"maxLength": 10
}
Format Keywords
The format
keyword is used to specify a format that a string must conform to. It takes a string, which is the format. Some common formats include:
"date"
: A date string in the formatYYYY-MM-DD
."time"
: A time string in the formatHH:MM:SS
."datetime"
: A datetime string in the formatYYYY-MM-DDTHH:MM:SSZ
."email"
: An email address string.
If a string does not conform to the specified format, it fails validation.
For example, the following schema specifies that a value must be a string representing a valid email address:
{
"type": "string",
"format": "email"
}
Validation Keywords for Arrays
Arrays are a common data type in JSON and often require validation to ensure that they conform to specific rules and constraints. JSON Schema provides several validation keywords for arrays that can be used to enforce these constraints. In this blog post, we will explore some of the most common validation keywords for arrays in JSON Schema.
Items Keyword
The items
keyword is used to specify the validation schema for each item in an array. It takes a single schema or an array of schemas. If a schema is provided, it is applied to all items in the array. 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"
}
}
Alternatively, the following schema specifies that a value must be an array where the first item is an integer and the second item is a string:
{
"type": "array",
"items": [
{"type": "integer"},
{"type": "string"}
]
}
Additional Items Keyword
The additionalItems
keyword is used to specify the validation schema for additional items in an array. It is used when the items
keyword is an array of schemas, and there are additional items in the array that do not have a corresponding schema. The additionalItems
keyword takes a schema, which is applied to all additional items in the array.
For example, the following schema specifies that a value must be an array where the first item is an integer and the second item is a string. Additional items in the array can be any type:
{
"type": "array",
"items": [
{"type": "integer"},
{"type": "string"}
],
"additionalItems": true
}
Min and Max Items Keywords
The minItems
and maxItems
keywords are used to specify the minimum and maximum number of items in an array, respectively. They take a non-negative integer, which is the minimum or maximum allowed number of items. If an array has fewer than the minItems
or more than the maxItems
, it fails validation.
For example, the following schema specifies that a value must be an array with at least 3 items and at most 5 items:
{
"type": "array",
"minItems": 3,
"maxItems": 5
}
Unique Items Keyword
The uniqueItems
keyword is used to specify whether all items in an array must be unique. It takes a boolean value. If set to true
, all items in the array must be unique. If set to false
or not included, items can be repeated.
For example, the following schema specifies that a value must be an array with unique items:
{
"type": "array",
"uniqueItems": true
}
Validation Keywords for Objects
Properties Keyword
The properties
keyword is used to define a schema for each property of an object. It takes an object where the keys are the property names and the values are schemas that describe the properties.
For example, the following schema specifies that a value must be an object with two properties: name
and age
. The name
property must be a string, and the age
property must be an integer.
{
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "integer"}
}
}
Additional Properties Keyword
The additionalProperties
keyword is used to specify the validation schema for additional properties in an object. It is used when the properties
keyword does not specify a schema for all properties of the object. The additionalProperties
keyword takes a schema, which is applied to all additional properties in the object.
For example, the following schema specifies that a value must be an object with two properties: name
and age
. Additional properties can be any type.
{
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "integer"}
},
"additionalProperties": true
}
Min and Max Properties Keywords
The minProperties
and maxProperties
keywords are used to specify the minimum and maximum number of properties in an object, respectively. They take a non-negative integer, which is the minimum or maximum allowed number of properties. If an object has fewer than the minProperties
or more than the maxProperties
, it fails validation.
For example, the following schema specifies that a value must be an object with at least 2 properties and at most 5 properties:
{
"type": "object",
"minProperties": 2,
"maxProperties": 5
}
Dependencies Keyword
The dependencies
keyword is used to specify dependencies between properties of an object. It takes an object where the keys are property names, and the values are either schemas or arrays of property names. If the key property is present in the object, then the corresponding schema or array of property names must also be present.
Validation keywords are a powerful tool for ensuring that JSON data conforms to certain rules and constraints. In this blog post, we’ve explored some of the most common validation keywords in JSON Schema validation, including required
, type
, enum
, minimum
and maximum
, and pattern
. By using these keywords in your JSON schemas, you can ensure that your JSON data is always valid and conforms to your specific requirements.
Reblogged this on QATechnicals and commented:
How to validate using JSON Schema
LikeLike