required & fail

required forces a value to be provided and fail aborts rendering with a custom message.

Syntax{{ required "a value is required" .Values.key }}

Sometimes a chart cannot work without a specific value. Rather than render broken YAML, stop with a clear error.

required

{{ required "message" .Values.key }} returns the value if set, or aborts the render with your message if it is empty. Perfect for mandatory settings like a database password or hostname.

fail

{{ fail "message" }} unconditionally stops rendering with an error. Combine it with if to enforce rules — for example, rejecting an invalid combination of values.

Example

Example · yaml
data:
  host: {{ required "ingress.host is required!" .Values.ingress.host | quote }}

{{- if and .Values.persistence.enabled (not .Values.persistence.size) }}
{{- fail "persistence.size must be set when persistence is enabled" }}
{{- end }}

When to use it

  • A chart author marks the database password as required so helm install fails with a clear message rather than deploying a broken application with an empty password.
  • A developer uses fail to abort rendering if the operator selects an unsupported storage class, providing an actionable error instead of a mysterious Pod startup failure.
  • A CI pipeline captures the non-zero exit code from a failed helm template --dry-run caused by required, blocking the deployment before any resources are created.

More examples

required for mandatory value

required aborts rendering with a descriptive error message when .Values.db.password or .Values.apiKey is absent or empty.

Example · yaml
# templates/secret.yaml
data:
  password: {{ required "db.password is required" .Values.db.password | b64enc | quote }}
  apiKey: {{ required "apiKey must be set" .Values.apiKey | b64enc | quote }}

fail for invalid option

Uses fail with a formatted message to abort rendering when service.type is not one of the allowed values.

Example · yaml
{{- $validTypes := list "ClusterIP" "NodePort" "LoadBalancer" }}
{{- if not (has .Values.service.type $validTypes) }}
  {{- fail (printf "service.type must be one of %v, got: %s" $validTypes .Values.service.type) }}
{{- end }}

Catch error in CI

Runs helm template in CI and checks the exit code; required and fail cause a non-zero exit, blocking the pipeline on misconfiguration.

Example · bash
# This will exit non-zero if required values are missing
helm template my-app ./chart \
  -f values-prod.yaml \
  --dry-run 2>&1

# Check exit code
if [ $? -ne 0 ]; then
  echo 'Chart validation failed'
  exit 1
fi

Discussion

  • Be the first to comment on this lesson.
required & fail — Helm | SoundsCode