Skip to main content
The type keyword validates that an instance matches one or more specified data types.

Syntax

The value of type must be either:
  • A string representing a single type
  • An array of strings representing multiple allowed types
If an array is used, it must be non-empty with unique elements.

Allowed Types

The following type values are valid:
  • "null" - JSON null value
  • "boolean" - JSON boolean (true or false)
  • "object" - JSON object
  • "array" - JSON array
  • "number" - JSON number (including integers and decimals)
  • "string" - JSON string
  • "integer" - A number with a zero fractional part

Validation Rules

Single type: An instance validates successfully if its type matches the specified type. Multiple types: An instance validates successfully if its type matches any of the types in the array.

Examples

Single Type

{
  "type": "string"
}
Valid instances:
"hello"
"123"
""
Invalid instances:
123
true
null

Integer Type

{
  "type": "integer"
}
Valid instances:
42
0
-10
Invalid instances:
3.14
"42"
true

Multiple Types

{
  "type": ["string", "number"]
}
Valid instances:
"hello"
42
3.14
Invalid instances:
true
null
{}

Nullable String

{
  "type": ["string", "null"]
}
Valid instances:
"hello"
null
Invalid instances:
123
true

Notes

  • The "integer" type is not a primitive JSON type but is provided as a convenient way to validate whole numbers
  • When using "integer", the value 3.0 is considered valid because it has a zero fractional part
  • The array form of type is useful for creating nullable types or allowing multiple data types

Build docs developers (and LLMs) love