Object type

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

{
  "type": "object"
}

{} - valid (object with nu properties)

{"prop1": "val1", "prop2": 2.5} - valid

12 - invalid (is integer/number)

null - invalid (is null)

"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.

  1. properties
  2. required
  3. dependencies
  4. minProperties
  5. maxProperties
  6. propertyNames
  7. patternProperties
  8. additionalProperties

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"
    }
  }
}

{"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 (a and b properties are missing)

{"a": 1, "b": 5} - invalid (a is not a string)

{"a": 1, "b": "text"} - invalid (a is not a string, 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"]
}

{"a": 1, "b": 2, "c": 3} - valid

{"a": 1, "b": null} - valid

{"a": 1, "c": 3} - invalid (missing property b)

{"c": 1, "d": 3} - invalid (missing both a and b properties)

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"
        }
      }     
    }
  }
}

{"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
}

{"a": "a", "b": "b", "c": "c"} - valid (3 > 2)

{"a": "a", "b": "b"} - valid (2 = 2)

{"a": "a"} - invalid (1 < 2)

{} - invalid (0 < 2)

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
}

{"a": "a", "b": "b"} - valid (2 = 2)

{"a": "a"} - valid (1 < 2)

{} - valid (0 < 2)

{"a": "a", "b": "b", "c": "c"} - invalid (3 > 2)

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
  }
}

{"prop1": 0, "prop2": "str"} - valid

{"prop": null} - valid

{} - valid

{"prop": 1, "a": 2} - invalid (length of "a" = 1 < 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"
    }
  }
}

{"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 matched)

{"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"
  }
}

{"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
}

{"a": "a", "b": "str"} - valid

{"a": 1} - valid

{} - valid (no properties to check)

{"a": "a", "c": 2} - invalid (c property is not allowed)

{"a": "a", "c": 2, "d": null} - invalid (c and d properties are not allowed)

{
  "type": "object",
  "patternProperties": {
    "^a": true,
    "^b": true  
  },
  "additionalProperties": false
}

{"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 e)

{"abc": "a", "Bcd": 2} - invalid (Bcd starts with B not b)

{
  "type": "object",
  "properties": {
    "a": true,
    "b": true
  },
  "patternProperties": {
    "^extra-": {
      "type": "string"
    }
  },
  "additionalProperties": {
    "type": "integer"
  }
}

{"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 integer)