In the Previous Post

We looked at what JSON Schema is. In this post, we cover how to define schemas that validate JSON data.



Validating Numbers

Number type validation

Set type to number to validate numeric values.

{
    "type": "number"
}

Integer type validation

Set type to integer to validate integer values.

{
  "type": "integer"
}

0 and negative values are allowed, but decimals like 12.34 and string values like "123" are not.

Range validation

  • minimum, maximum: lower and upper numeric bounds
  • exclusiveMinimum, exclusiveMaximum
    • If the value is true, the minimum/maximum boundary itself is excluded.
    • If the value is false, the boundary is included.

The following schema allows numbers greater than 0 and less than 255 (255 excluded).

{
    "type": "number",
    "minimum": 1,
    "maximum": 255,
    "exclusiveMaximum": true
}

Multiples validation

Use multipleOf to validate whether a number is a multiple of a given value. The following schema checks multiples of 5 such as 0, 5, 10, and 15.

{
    "type": "number",
    "multipleOf": 5
}



Validating Strings

String type validation

To validate a string, set type to string.

{
    "type": "string"
}

String length validation

Use minLength and maxLength.

{
    "type": "string",
    "minLength": 3,
    "maxLength": 15
}

Regular expression validation

Use pattern to validate against a regex. The following example allows only madplay.com or kimtaeng.com.

{
    "type": "string",
    "pattern": "(madplay|kimtaeng)\\.com"
}

String format validation

Use format to validate a known string format.

{
    "type": "string",
    "format": "date"
}

Examples by format:

  • Date and time
    • date-time: 1991-02-04T00:31:00+09:00
    • time: 00:31:00+09:00
    • date: 1991-02-04
  • Email
    • email: kimtaeng@madplay.com
  • Hostname
    • hostname: kimtaeng-madplay.com
  • IP
    • ipv4: 123.123.123.123
    • ipv6: 2001:0DB8:0000:0000:0000:0000:1428:57ab

Media type validation

Use contentEncoding and contentMediaType to validate MIME-related payloads.

{
  "type": "string",
  "contentEncoding": "base64",
  "contentMediaType": "application/json"
}

With this schema, base64 strings such as "eyJhIjogMX0=" and "bnVsbA==" pass. They decode to {"a":1} and "null" respectively.

By contrast, non-base64 values like "2-3-4", or decoded content that is not valid JSON (for example "e2E6IDF9" -> "{a:1}") fail.



Validating Arrays

Array type validation

Set type to array.

{
    "type": "array"
}

Array item validation

Use items to validate elements in the array. The following schema allows only arrays whose elements are all integers (or empty arrays).

{
    "type": "array",
    "items": {
        "type": "integer"
    }
}

You can also validate mixed array types. In the schema below, the first item is integer, the second and third are strings, and the third string must be at most 10 characters.

{
    "type": "array",
    "items": [
        {
            "type": "integer"
        },
        {
            "type": "string"
        },
        {
            "type": "string",
            "maxLength": 10
        }
    ],
    "additionalItems": false
}

If additionalItems is false, items beyond the declared list are not allowed. In this example, only the three declared positions are valid.

Duplicate item validation

Use uniqueItems to validate duplicate elements. If true, duplicates are rejected.

{
    "type": "array",
    "uniqueItems": true
}

With this schema, the following JSON fails because it contains duplicate objects.

[
  {
    "id": 1,
    "author": "madplay"
  },
  {
    "id": 1,
    "author": "madplay"
  }
]

Array length validation

Like strings, arrays can enforce minimum and maximum length.

{
    "type": "array",
    "minItems": 5,
    "maxItems": 10
}



Validating Objects

Object type validation

Set type to object to validate object values.

{
    "type": "object"
}

Property validation

You can validate object properties. The following schema allows string id, string title, and integer type.

{
    "type": "object",
    "properties": {
        "id": {
            "type": "string"
        },
        "title": {
            "type": "string"
        },
        "type": {
            "type": "integer"
        }
    },
    "required": ["id", "title"]
}

required defines mandatory properties. If id or title is missing, validation fails.

Property name validation

Property names can also be validated. The schema below requires all property names to start with a, b, or c.

{
    "type": "object",
    "propertyNames": {
        "pattern": "^[a-c].+"
    }
}

Property dependency validation

Use dependencies to define property dependencies.

{
    "type": "object",
    "properties": {
        "customerName": {
            "type": "string"
        },
        "creditCardNumber": {
            "type": "string"
        },
        "billingAddress": {
            "type": "string"
        }
    },
    "dependencies": {
        "creditCardNumber": ["billingAddress"]
    }
}

This means creditCardNumber depends on billingAddress. So creditCardNumber alone is not allowed.

Example that fails:

{
    "customerName": "kimtaeng",
    "creditCardNumber": "12341234"
}

If both dependent fields are absent, it can still pass.

{
    "customerName": "kimtaeng"
}

Schema dependency validation

This is similar to property dependencies, but instead of listing required peer properties, it attaches an additional schema. The schema below defines objects with title and author. If author exists, then integer articleNumber must also exist.

{
    "type": "object",
    "properties": {
        "title": {
            "type": "string"
        },
        "author": {
            "type": "string"
        }
    },
    "dependencies": {
        "author": {
            "properties": {
                "articleNumber": {
                    "type": "integer"
                }
            },
            "required": [
                "articleNumber"
            ]
        }
    }
}

The following JSON fails because author exists without articleNumber.

{
  "title": "MadPlay's MadLife.",
  "author": "kimtaeng"
}

The next case also fails because articleNumber is not an integer.

{
  "title": "MadPlay's MadLife.",
  "author": "kimtaeng",
  "articleNumber": "2"
}

By contrast, if author is missing, validation passes even when articleNumber is not integer.

{
  "title": "MadPlay's MadLife.",
  "articleNumber": "2"
}

Property count validation

You can set minimum and maximum property counts.

{
    "type": "object",
    "minProperties": 2,
    "maxProperties": 3
}


Next

In this post, we covered basic JSON Schema declaration patterns for validating JSON data. In the next post, we will go deeper into combining schema rules and adding conditional logic.