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' | sortdefaults: 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.-]+)?