Specify Default Values in YAML Schema

Learn how to define default values in YAML Schema for optional fields. Covers the default keyword, interaction with required, schema-aware merging, and tool support for defaults.

Validation Rules

Detailed Explanation

Default Values in YAML Schema

The default keyword in YAML Schema specifies the value to use when a property is absent from the document. This is purely informational in JSON Schema specification, but many tools and validators use it to auto-fill missing values.

Example JSON Input

{
  "host": "0.0.0.0",
  "port": 3000,
  "workers": 4,
  "logLevel": "info",
  "gracefulShutdown": true,
  "shutdownTimeout": 30
}

Generated YAML Schema with Defaults

type: object
properties:
  host:
    type: string
    default: "0.0.0.0"
  port:
    type: integer
    default: 3000
    minimum: 1
    maximum: 65535
  workers:
    type: integer
    default: 4
    minimum: 1
  logLevel:
    type: string
    default: info
    enum: [trace, debug, info, warn, error, fatal]
  gracefulShutdown:
    type: boolean
    default: true
  shutdownTimeout:
    type: integer
    default: 30
    minimum: 1
    description: Seconds to wait before force-killing processes
required:
  - host
  - port

Default vs Required

These two concepts interact in important ways:

Combination Behavior
Required + no default Field must be present; error if missing
Required + default Field must be present; default is informational only
Optional + default Field may be absent; tools may inject default
Optional + no default Field may be absent; no fallback

Schema-Aware Configuration Merging

Many tools merge defaults from the schema before validation:

# User provides (minimal config):
port: 9090

# After schema-aware merge:
host: "0.0.0.0"     # default applied
port: 9090           # user value preserved
workers: 4           # default applied
logLevel: info       # default applied
gracefulShutdown: true  # default applied
shutdownTimeout: 30    # default applied

Defaults for Nested Objects

properties:
  logging:
    type: object
    default:
      level: info
      format: json
    properties:
      level:
        type: string
        default: info
      format:
        type: string
        default: json

Note that both the object itself and its individual properties can have defaults.

Tool Support

Tool Uses defaults?
ajv (useDefaults option) Yes
yq No (validation only)
pykwalify Yes
Kubernetes admission controllers Depends on webhook
Helm Yes (values.yaml)

Best Practices

  1. Always provide defaults for optional fields -- makes the schema self-documenting
  2. Use realistic values -- defaults should be production-safe, not placeholder values
  3. Document default behavior -- add a description explaining the default choice
  4. Test default-only configs -- ensure the service starts correctly with only required fields

Use Case

Helm charts and Kubernetes operators rely heavily on schema defaults to provide sensible configuration out of the box. By generating a YAML schema with defaults from a known-good JSON configuration, you create a template that lets users override only the values they care about while inheriting safe defaults for everything else.

Try It — JSON to YAML Schema

Open full tool