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.
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
- Always provide defaults for optional fields -- makes the schema self-documenting
- Use realistic values -- defaults should be production-safe, not placeholder values
- Document default behavior -- add a
descriptionexplaining the default choice - 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.