Sub topic 13
Validating values with values.schema.json
This chapter assumes you can read a chart's values.yaml, override values with -f or --set, and run helm template and helm lint. Without validation, a typo like replicaCount: "two" renders into a manifest, and the failure shows up later in the cluster. A schema catches it before anything is rendered.
How the schema works
Place a file named values.schema.json at the chart root, next to Chart.yaml. It is a JSON Schema document. Helm validates the final merged values (chart defaults plus every -f file plus every --set) against it.
Helm runs this check during helm install, helm upgrade, helm lint and helm template. If validation fails, Helm stops and prints which value broke which rule. No manifests are produced and nothing is sent to the cluster.
A working schema
This schema requires an image object with a string repository, and requires replicaCount to be an integer of at least 1.
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"required": ["image"],
"properties": {
"replicaCount": { "type": "integer", "minimum": 1 },
"image": {
"type": "object",
"required": ["repository"],
"properties": {
"repository": { "type": "string" }
}
}
}
}Each part does one job:
typerestricts the kind of value:object,string,integer,boolean,array.requiredlists keys that must be present. It sits on the object that contains those keys.propertiesdescribes each known key, and can nest.minimumand similar keywords restrict the range of a number.
Seeing it fail
Run the chart with an invalid override:
helm template my-release ./webapp --set replicaCount=0Helm reports an error that names the failing path, such as replicaCount, and the rule it broke. Fix the value and the render succeeds. Run helm lint ./webapp in CI to catch the same problem in a pipeline.
Common errors
--setmakes strings.--set replicaCount=3is parsed as an integer, so it passes. A quoted value in a values file, such asreplicaCount: "3", is a string and fails"type": "integer".- Required keys need a source. If the schema requires
image.repositorybutvalues.yamlleaves it out, every install fails until a user supplies it. That is often the intent. - Unknown keys are allowed by default. A misspelled key like
replicaContpasses unless you add"additionalProperties": falseto that object. Add it only where you want strict checking, because it also rejects keys that a subchart or aglobalblock may need. - Invalid JSON breaks everything. A trailing comma makes Helm fail on every command. Check the file with
helm lint.
Schema or `required`?
Use the schema for shape and type: what a value is and what range it may take. Use the template function required for a value that matters only in one template and where you want a custom message. The two work together, and the schema runs first.
Exercise
A user's file contains replicaCount: "2" and the schema declares replicaCount as "type": "integer". What happens on helm install, and does it matter that the template would have rendered the number correctly?
Show answer
Helm rejects the install during validation, before any template renders, because "2" in YAML is a string and not an integer. Whether the template would have produced valid output does not matter. The fix is to write replicaCount: 2 without quotes.
Exercise
You want Helm to report a misspelled top-level key such as replicaCont. Which schema keyword do you add, and what is the risk?
Show answer
Add "additionalProperties": false to the top-level object. The risk is that it also rejects legitimate keys not listed in properties, such as global or the values block for a subchart, so you must list those keys too.