Generic validation keywords

These keywords allow you to validate an instance by checking the data type or by checking if the data value equals to a predefined value.

Validation keywords

The following keywords are supported by any type, and evaluated in the presented order. All keywords are optional.

  1. type
  2. const
  3. enum

type

The type keyword specifies the data type that a schema will use. This keyword is not mandatory, and the value of keyword can be a string representing a valid data type, or an array of strings representing valid data types.

{
  "type": "string"
}

"some text" - valid

"" - valid (empty string)

12 - invalid (is integer/number)

null - invalid (is null)

You can use multiple types at once to restrict accepted data types, or you can omit the type keyword to accept any type. The order of types in the array doesn’t matter, but you should not put the same type more than once.

{
  "type": ["object", "null"]
}

{"a": 1} - valid (is object)

null - valid (is null)

"1, 2, 3" - invalid (is string)

[{"a": 1}, {"b": 2}] - invalid (is array)

{
  "type": ["number", "string", "null"]
}

-10.5 - valid (is number)

"some string" - valid (is string)

null - valid (is null)

false - invalid (is boolean)

{"a": 1} - invalid (is object)

[1, 2, 3] - invalid (is array)

const

An instance validates against this keyword if its value equals to the value of this keyword. The value of this keyword can be anything.

{
  "const": "test"
}

"test" - valid

"Test" - invalid

"tesT" - invalid

3.4 - invalid

{
  "const": {
    "a": 1,
    "b": "2"
  }
}

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

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

{"a": 1, "b": "2", "c": null} - invalid

5.10 - invalid

enum

An instance validates against this keyword if its value equals can be found in the items defined by the value of this keyword. The value of this keyword must be an array containing anything. An empty array is not allowed.

{
  "enum": ["a", "b", 1, null]
}

"a" - valid

"b" - valid

1 - valid

null - valid

"A" - invalid

-1 - invalid

false - invalid

["a", "b", 1, null] - invalid