Boolean Type Validation with JSON Schema

Validate boolean values in JSON Schema. Learn to enforce true/false with const, set defaults, and avoid common pitfalls with truthy/falsy values.

Basic Types

JSON Schema

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "isActive": {
      "type": "boolean"
    },
    "emailNotifications": {
      "type": "boolean",
      "default": true
    },
    "acceptedTerms": {
      "type": "boolean",
      "const": true
    }
  },
  "required": ["isActive", "acceptedTerms"]
}

Test Data

{
  "isActive": true,
  "emailNotifications": false,
  "acceptedTerms": true
}

Detailed Explanation

Validating Booleans in JSON Schema

The boolean type accepts exactly two values: true and false. Unlike many programming languages, JSON Schema does not treat 0, 1, "true", "false", or null as booleans — only the literal JSON tokens true and false pass validation.

Basic Boolean Schema

{ "type": "boolean" }

This rejects strings like "true", numbers like 1, and null. Only the bare keywords true and false are valid.

Enforcing a Specific Value with const

Sometimes you need a boolean that must always be true — for example, confirming that a user has accepted terms of service:

{
  "type": "boolean",
  "const": true
}

The const keyword restricts the value to a single allowed literal. If the user sends false, validation fails with a clear error message.

How the Example Schema Works

The schema defines a user preferences object with three boolean fields:

  1. isActive — a required boolean indicating whether the account is active. Both true and false are valid.
  2. emailNotifications — an optional boolean with default: true. If the client omits this field, the application should treat it as true. Note that JSON Schema validators do not insert defaults — the default keyword is metadata for documentation and code generators.
  3. acceptedTerms — a required boolean constrained to true by the const keyword. This ensures the user explicitly accepts the terms before submission.

The test data passes because isActive is a boolean, emailNotifications is a boolean, and acceptedTerms is exactly true.

Common Mistakes

  • Sending "true" as a string: Form data and query parameters often arrive as strings. Your API must parse them before validation, or the schema will reject them.
  • Relying on default insertion: The default keyword does not modify data during validation. Your application code or a pre-processing step must apply defaults.
  • Nullable booleans: If a boolean can also be null, use "type": ["boolean", "null"] instead of plain "type": "boolean".

When to Use const vs. enum

For booleans, const: true is equivalent to enum: [true]. Use const when there is exactly one allowed value — it produces clearer error messages and is easier to read.

Use Case

Use boolean validation for feature flags, user consent fields, notification preferences, and toggle settings in REST APIs. The const constraint is especially valuable for legal acceptance flows where the API must guarantee the user explicitly opted in.

Try It — JSON Schema Validator

Open full tool