TExcelTAD
Home › Kubernetes Packaging: Helm & Kustomize › Validating Values with a Schema

Validating Values with a Schema

A typo in a values file (replicaCont: 5) is silently ignored: the template reads replicaCount, finds the default, and production runs two replicas instead of five. A wrong type (replicaCount: "five") produces a manifest the API server rejects, minutes into a deploy. A values schema catches both at render time.

values.schema.json

Put a JSON Schema file named values.schema.json next to values.yaml. Helm validates the merged values against it on install, upgrade, template and lint, before rendering anything.

bash · try it yourself
mkdir -p linkly/templates && cd linkly
printf 'apiVersion: v2\nname: linkly\nversion: 0.4.0\n' > Chart.yaml
cat > values.yaml <<'EOF'
replicaCount: 2
image:
  repository: ghcr.io/acme/linkly
  tag: "1.4.0"
logLevel: info
EOF
cat > values.schema.json <<'EOF'
{
  "$schema": "https://json-schema.org/draft-07/schema#",
  "type": "object",
  "additionalProperties": false,
  "required": ["replicaCount", "image", "logLevel"],
  "properties": {
    "replicaCount": {"type": "integer", "minimum": 1},
    "image": {
      "type": "object",
      "additionalProperties": false,
      "required": ["repository", "tag"],
      "properties": {
        "repository": {"type": "string", "minLength": 1},
        "tag": {"type": "string", "pattern": "^[0-9]+\\.[0-9]+\\.[0-9]+(-[0-9A-Za-z.-]+)?
quot;} } }, "logLevel": {"enum": ["debug", "info", "warn", "error"]} } } EOF printf 'apiVersion: v1\nkind: ConfigMap\nmetadata:\n name: linkly\ndata:\n replicas: "{{ .Values.replicaCount }}"\n' > templates/cm.yaml helm lint . --quiet && echo "defaults: valid" echo "--- a typo in a values file" printf 'replicaCont: 5\n' > typo.yaml helm template demo . -f typo.yaml echo "--- wrong types and values" helm template demo . --set replicaCount=0 --set logLevel=verbose --set image.tag=latest 2>&1 | grep '^- at' | sort
defaults: valid
--- a typo in a values file
Error: values don't meet the specifications of the schema(s) in the following chart(s):
linkly:
- at '': additional properties 'replicaCont' not allowed

--- wrong types and values
- at '/image/tag': 'latest' does not match pattern '^[0-9]+\\.[0-9]+\\.[0-9]+(-[0-9A-Za-z.-]+)?
  


- at '/logLevel': value must be one of 'debug', 'info', 'warn', 'error'
- at '/replicaCount': minimum: got 0, want 1

Every violation is reported at once, with the JSON path of the offending value. (The order of the violations varies from run to run, so the last command keeps only those lines and sorts them.) additionalProperties: false is what turns a typo into an error; without it, unknown keys are allowed.

What to put in the schema

  • Types for everything users set, especially numbers and booleans that --set might turn into strings or vice versa.
  • required for values the chart can't work without, rather than a required call buried in a template.
  • enum for closed choices (log levels, service types, pullPolicy).
  • pattern for formats: image tags, hostnames, durations.
  • additionalProperties: false at the levels you own. Leave it off for maps users fill freely (podAnnotations, env, nodeSelector), or describe their values with "additionalProperties": {"type": "string"}, which also forces annotation values to be strings.
  • description on each property: IDEs with a YAML language server show it when editing values files, and tools such as helm-docs can render it.

Tip: Writing the schema by hand for a large chart is tedious. Plugins and generators exist (for example the helm-values-schema-json plugin generates one from annotated values.yaml comments), but read the result: a generated schema only knows the types of the defaults.

Schemas and subcharts

Each chart validates its own section: a subchart's values.schema.json checks the values under the subchart's key in the parent, after merging. A parent can't relax a subchart's schema, which is one reason to keep library-like subcharts permissive.

Skipping validation

--skip-schema-validation (on install, upgrade, template and lint) turns the check off. It exists for emergencies with a broken third-party schema; needing it for your own chart means the schema is wrong.

bash · try it yourself
mkdir -p c/templates && cd c
printf 'apiVersion: v2\nname: c\nversion: 0.1.0\n' > Chart.yaml
printf 'replicaCount: 2\n' > values.yaml
printf '{"type": "object", "properties": {"replicaCount": {"type": "integer"}}}\n' > values.schema.json
printf 'replicas: {{ .Values.replicaCount }}\n' > templates/r.yaml
helm template d . --set-string replicaCount=3 2>&1 | head -3
helm template d . --set-string replicaCount=3 --skip-schema-validation | tail -1
Error: values don't meet the specifications of the schema(s) in the following chart(s):
c:
- at '/replicaCount': got string, want integer
replicas: 3

Test yourself

Which schema keyword turns a misspelt values key into an error?

  1. required
  2. additionalProperties: false
  3. enum
  4. minimum

Against what does Helm validate values.schema.json?

  1. Only values.yaml
  2. Only the -f files
  3. The merged values (defaults, files and --set)
  4. The rendered manifests

Should podAnnotations have additionalProperties: false?

  1. Yes, always
  2. No: it is a free-form map; constrain its values (e.g. to strings) instead
  3. Only in production
  4. Schemas can't describe maps