YAML Schema Boolean and Null Types from JSON

Learn how JSON boolean and null values map to YAML Schema type definitions. Covers boolean defaults, nullable types, oneOf for mixed types, and YAML-specific truthy/falsy pitfalls.

Type Mapping

Detailed Explanation

Boolean and Null Type Mapping

JSON true/false values map to type: boolean in YAML Schema. JSON null introduces nullable types, which require special handling in the schema definition.

Boolean Properties

{
  "debug": true,
  "enableCache": false,
  "verbose": true
}
type: object
properties:
  debug:
    type: boolean
    default: false
  enableCache:
    type: boolean
    default: true
  verbose:
    type: boolean
    default: false

Nullable Properties

When a JSON field is null, the schema must allow the null type alongside the expected type:

{
  "name": "Alice",
  "nickname": null,
  "age": 30,
  "bio": null
}
type: object
properties:
  name:
    type: string
  nickname:
    type:
      - string
      - "null"
  age:
    type: integer
  bio:
    type:
      - string
      - "null"

Draft 2020-12 Nullable Syntax

In JSON Schema draft 2020-12, nullable types use an array syntax:

nickname:
  type: [string, "null"]

In earlier drafts, you use oneOf:

nickname:
  oneOf:
    - type: string
    - type: "null"

YAML Boolean Pitfall

YAML parsers interpret several values as boolean: yes, no, on, off, true, false. In YAML Schema definitions, always quote "null" to prevent the parser from interpreting it as a special value:

# Correct
type: [string, "null"]

# Dangerous -- YAML may parse null as empty
type: [string, null]

Boolean with Description

Adding descriptions to boolean fields improves schema usability:

debug:
  type: boolean
  default: false
  description: >
    Enable debug mode. When true, the application logs
    detailed request/response bodies and stack traces.

Combining Boolean with Conditional Logic

properties:
  tls:
    type: boolean
  certPath:
    type: string
if:
  properties:
    tls:
      const: true
then:
  required:
    - certPath

This schema makes certPath required only when tls is true, enabling conditional validation based on boolean flags.

Use Case

Feature flags, debug toggles, and optional configuration fields frequently use booleans and nullable values. A YAML schema that correctly handles boolean defaults and nullable types prevents the common YAML pitfall where 'yes'/'no' strings are accidentally interpreted as booleans, which is a frequent source of configuration bugs.

Try It — JSON to YAML Schema

Open full tool