The object type is used for validating key-value maps (objects).

{
  "type": "object"
}
Input Status
{} valid - object with no properties
{"prop1": "val1", "prop2": 2.5} invalid
12 invalid - is integer/number
"some text" invalid - is string

Validation keywords

The following keywords are supported by the object type, and evaluated in the following order: required, dependencies, minProperties, maxProperties, propertyNames, properties, patternProperties, additionalProperties. All keywords are optional.

properties

An object is valid against this keyword if every property that is present in both the object and the value of this keyword, validates against the corresponding schema. The value of this keyword must be an object, where properties must contain valid json schemas (objects or booleans). Only the property names that are present in both the object and the keyword value are checked.

{
  "type": "object",
  "properties": {
    "a": {
      "type": "string"
    },
    "b": {
      "type": "integer"
    }
  }
}
Input Status
{"a": "str", "b": 5} valid
{"a": "str"} valid - a is a string
{"b": 5, "c": null} valid - c is an integer
{"prop1": 0, "prop2": "str"} valid - both a and b properties are missing
{"a": 1, "b": 5} invalid - a is not a string
{"a": 1, "b": "text"} invalid - a is not string and b is not an integer

required

An object is valid against this keyword if it contains all property names (keys) specified by the value of this keyword. The value of this keyword must be a non-empty array of strings representing property names.

{
  "type": "object",
  "required": ["a", "b"]
}
Input Status
{"a": 1, "b": 2, "c": 3} valid
{"a": 1, "b": null} valid
{"a": 1, "c": 3} invalid - property b is missing
{"c": 1, "d": 3} invalid - both a and b properties are missing

dependencies

An object is valid against this keyword if it mets all dependencies specified by this keyword value. The value of this keyword must be an object, where property values can be:

  • objects representing valid json schemas, and the whole object must match the entire schema
  • arrays of strings representing property names, then the object must contain all property names

Only property names (from this keyword value) that are also present in the object are checked.

{
  "type": "object",
  "dependencies": {
    "a": ["b", "c"],
    "c": {
      "type": "object",
      "properties": {
        "b": {
          "type": "integer"
        }
      }     
    }
  }
}
Input Status
{"c": 1} valid - b is not required
{"c": 1, "b": 4} valid
{"a": 1, "b": 4, "c": 3, "d": true} valid
{"b": "str"} valid - no dependencies
{"c": 1, "b": "str"} invalid - b must be an integer
{"a": 1, "b": "str"} invalid - c is not present

minProperties

An object is valid against this keyword if the number of properties it contains is greater then, or equal to, the value of this keyword. The value of this keyword must be a non-negative integer. Using 0 as a value has no effect.

{
  "type": "object",
  "minProperties": 2
}
Input Status
{"a": "a", "b": "b", "c": "c"} valid - has 3 properties
{"a": "a", "b": "b"} valid - has 2 properties
{"a": "a"} invalid - has a single property
{} invalid - has no properties

maxProperties

An object is valid against this keyword if the number of properties it contains is lower then, or equal to, the value of this keyword. The value of this keyword must be a non-negative integer. Using 0 as a value means that the object must be empty (no properties).

{
  "type": "object",
  "maxProperties": 2
}
Input Status
{"a": "a", "b": "b"} valid - has 2 properties
{"a": "a"} valid - has a single property
{} valid - has no properties
{"a": "a", "b": "b", "c": "c"} invalid - has more than 2 properties

propertyNames

An object is valid against this keyword if every property name (key) is valid against the value of this keyword. The value of this keyword must be a valid json schema (an object or a boolean).

Please note that the value of propertyNames (the schema) will always test strings.

{
  "type": "object",
  "propertyNames": {
    "type": "string",
    "minLength": 2
  }
}
Input Status
{"prop1": 0, "prop2": "str"} valid
{} valid
{"prop": 1, "a": 2} invalid - length of a is smaller than 2

patternProperties

An object is valid against this keyword if every property where a property name (key) matches a regular expression from the value of this keyword, is also valid against the corresponding schema. The value of this keyword must an object, where the keys must be valid regular expressions and the corresponding values must be valid json schemas (object or boolean).

{
  "type": "object",
  "patternProperties": {
    "^str-": {
      "type": "string"
    },
    "^int-": {
      "type": "integer"
    }
  }
}
Input Status
{"str-a": "a"} valid
{"int-i": 2} valid
{"int-i": 2, "str-a": "a", "other": [1, 2]} valid - other property is not matched
{"other": "a"} valid - no property was match
{"str-a": "a", "str-b": 2} invalid - str-b property is integer, not string
{"str-a": "a", "int-b": 2.5} invalid - int-b property is float, not integer

additionalProperties

An object is valid against this keyword if all unchecked properties are valid against the schema defined by the value of this keyword. Unchecked properties are the properties not checked by the properties and patternProperties keywords (if a property name is not present in properties keyword and doesn’t match any regular expression defined by patternProperties keyword, then it is considered unchecked). The value of this keyword must be a valid json schema (object or boolean).

To be more concise, if we have unchecked properties:

  • if the value of this keyword is true, is always valid
  • if the value is false, is never valid
  • if the value contains an object (schema), every property must be valid against that schema.
{
  "type": "object",
  "additionalProperties": {
    "type": "string"
  }
}
Input Status
{"a": "a", "b": "str"} valid
{} valid - no properties to check
{"str-a": "a", "int-b": 2} invalid - int-b is integer, not string
{
  "type": "object",
  "properties": {
    "a": true,
    "b": true  
  },
  "additionalProperties": false
}
Input Status
{"a": "a", "b": "str"} valid
{"a": 1} valid
{} valid - no properties to check
{"a": "a", "c": 2} invalid - property c is not allowed
{"a": "a", "c": 2, "d": null} invalid - properties c and d are not allowed
{
  "type": "object",
  "patternProperties": {
    "^a": true,
    "^b": true  
  },
  "additionalProperties": false
}
Input Status
{"a": "a", "b": "str"} valid
{"aAA": "a", "bBB": "str"} valid
{"abc": "a"} valid
{} valid - no properties to check
{"abc": "a", "extra": 2} invalid - extra starts with an e
{"abc": "a", "Bcd": 2} invalid - Bcd starts with a B instead of b
{
  "type": "object",
  "properties": {
    "a": true,
    "b": true
  },
  "patternProperties": {
    "^extra-": {
      "type": "string"
    }
  },
  "additionalProperties": {
    "type": "integer"
  }
}
Input Status
{"a": "a", "b": "str"} valid
{"a": 1, "extra-a": "yes"} valid
{"a": 1, "extra-a": "yes", "other": 1} valid
{} valid - no properties to check
{"a": "a", "extra": 3.5, "other": null} invalid - extra and other must be integers
{"Extra-x": "x"} invalid - Extra-x does not start with extra-, so it must be an integer