Skip to main content

Overview

Validation keywords in a schema impose requirements for successful validation of an instance. These keywords are all assertions without any annotation behavior.

Any Instance Type

These keywords apply to instances of any type.

type

The type keyword validates that an instance matches one of the specified JSON types. Value: String or array of strings Valid type values:
  • "null" - JSON null
  • "boolean" - JSON boolean
  • "object" - JSON object
  • "array" - JSON array
  • "number" - JSON number (including integers and floats)
  • "string" - JSON string
  • "integer" - Any number with a zero fractional part
{
  "type": "string"
}
If type is an array, the instance validates if its type matches any of the types in the array.

enum

The enum keyword validates that an instance value equals one of the specified values. Value: Array (should have at least one element; elements should be unique)
{
  "enum": ["red", "green", "blue", null, 42]
}
Elements in the array can be of any type, including null. The instance must be exactly equal to one of the enum values.

const

The const keyword validates that an instance value equals a specific constant value. Value: Any JSON value, including null
{
  "const": "active"
}
This is functionally equivalent to an enum with a single value.

Numeric Instances

These keywords apply to instances of type number or integer.

multipleOf

Validates that a number is a multiple of the specified value. Value: Number (must be strictly greater than 0)
{
  "type": "number",
  "multipleOf": 0.5
}
A numeric instance is valid only if division by this keyword’s value results in an integer.
  • Value 10 with multipleOf: 5 → Valid (10 / 5 = 2)
  • Value 7 with multipleOf: 3 → Invalid (7 / 3 = 2.333…)
  • Value 1.5 with multipleOf: 0.5 → Valid (1.5 / 0.5 = 3)

maximum

Validates that a number is less than or equal to the specified maximum. Value: Number
{
  "type": "number",
  "maximum": 100
}
The instance validates if it is less than or exactly equal to maximum.

exclusiveMaximum

Validates that a number is strictly less than the specified maximum. Value: Number
{
  "type": "number",
  "exclusiveMaximum": 100
}
The instance validates if it is strictly less than (not equal to) exclusiveMaximum.

minimum

Validates that a number is greater than or equal to the specified minimum. Value: Number
{
  "type": "number",
  "minimum": 0
}
The instance validates if it is greater than or exactly equal to minimum.

exclusiveMinimum

Validates that a number is strictly greater than the specified minimum. Value: Number
{
  "type": "number",
  "exclusiveMinimum": 0
}
The instance validates if it is strictly greater than (not equal to) exclusiveMinimum.
{
  "type": "number",
  "minimum": 0,
  "maximum": 100
}

String Instances

These keywords apply to instances of type string.

maxLength

Validates that a string’s length is less than or equal to the specified value. Value: Non-negative integer
{
  "type": "string",
  "maxLength": 50
}
The length of a string is defined as the number of Unicode code points, not bytes or grapheme clusters.

minLength

Validates that a string’s length is greater than or equal to the specified value. Value: Non-negative integer
{
  "type": "string",
  "minLength": 1
}
Omitting this keyword has the same behavior as a value of 0.

pattern

Validates that a string matches a regular expression. Value: String (should be a valid regular expression)
{
  "type": "string",
  "pattern": "^[A-Z][a-z]+$"
}
Regular expressions are not implicitly anchored. Use ^ and $ to anchor the pattern to the start and end of the string.
{
  "type": "string",
  "pattern": "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$"
}

Array Instances

These keywords apply to instances of type array.

maxItems

Validates that an array’s size is less than or equal to the specified value. Value: Non-negative integer
{
  "type": "array",
  "maxItems": 10
}

minItems

Validates that an array’s size is greater than or equal to the specified value. Value: Non-negative integer
{
  "type": "array",
  "minItems": 1
}
Omitting this keyword has the same behavior as a value of 0.

uniqueItems

Validates that all elements in an array are unique. Value: Boolean
{
  "type": "array",
  "uniqueItems": true
}
  • If true: All array elements must be unique
  • If false (or omitted): Duplicate elements are allowed
[1, 2, 3, 4, 5]

Object Instances

These keywords apply to instances of type object.

maxProperties

Validates that an object’s number of properties is less than or equal to the specified value. Value: Non-negative integer
{
  "type": "object",
  "maxProperties": 10
}

minProperties

Validates that an object’s number of properties is greater than or equal to the specified value. Value: Non-negative integer
{
  "type": "object",
  "minProperties": 1
}
Omitting this keyword has the same behavior as a value of 0.

required

Validates that an object contains all specified properties. Value: Array of strings (elements must be unique)
{
  "type": "object",
  "required": ["name", "email"]
}
An object validates if every item in the required array is the name of a property in the instance.

dependentRequired

Specifies properties that are required if another specific property is present. Value: Object where values are arrays of strings
{
  "type": "object",
  "properties": {
    "creditCard": { "type": "string" },
    "billingAddress": { "type": "string" }
  },
  "dependentRequired": {
    "creditCard": ["billingAddress"]
  }
}
In this example, if creditCard is present, then billingAddress must also be present.
Conditional Requirements
{
  "type": "object",
  "properties": {
    "hasLicense": { "type": "boolean" },
    "licenseNumber": { "type": "string" },
    "licenseExpiry": { "type": "string" }
  },
  "dependentRequired": {
    "licenseNumber": ["hasLicense"],
    "licenseExpiry": ["hasLicense", "licenseNumber"]
  }
}

Complete Example

Here’s a comprehensive example combining multiple validation keywords:
{
  "type": "object",
  "required": ["username", "email", "age"],
  "properties": {
    "username": {
      "type": "string",
      "minLength": 3,
      "maxLength": 20,
      "pattern": "^[a-zA-Z0-9_]+$"
    },
    "email": {
      "type": "string",
      "format": "email"
    },
    "age": {
      "type": "integer",
      "minimum": 0,
      "maximum": 150
    },
    "tags": {
      "type": "array",
      "minItems": 1,
      "maxItems": 5,
      "uniqueItems": true,
      "items": {
        "type": "string"
      }
    },
    "status": {
      "enum": ["active", "inactive", "pending"]
    }
  },
  "maxProperties": 10
}

Build docs developers (and LLMs) love