Sub topic 10
Validating values with a JSON schema
This chapter assumes you can read a chart's values.yaml and override values with -f and --set. It also assumes you know basic JSON.
Why validate values
Helm does not check the types of values by default. If a user sets replicaCount to "three", the template renders a broken manifest, and the error appears only when Kubernetes rejects it. A values.schema.json file moves that failure earlier. Helm checks the merged values against the schema and stops with a message that names the bad key.
The schema is a standard JSON Schema document, and Helm accepts draft-07. Put it in the chart root next to Chart.yaml and values.yaml.
When Helm applies the schema
Helm validates in four commands: helm install, helm upgrade, helm lint, and helm template. It validates after it merges the chart defaults, any -f files, and any --set flags. Rendering starts only if validation passes.
Because defaults are merged first, a required key that has a default in values.yaml always passes the check.
A starting schema
This schema requires replicaCount and says it must be an integer of at least 1.
{
"$schema": "https://json-schema.org/draft-07/schema#",
"type": "object",
"required": ["replicaCount"],
"properties": {
"replicaCount": { "type": "integer", "minimum": 1 }
}
}Run it against a bad value:
helm lint ./webapp --set replicaCount=0
helm template my-release ./webapp --set replicaCount=0Both commands fail. The message lists the chart and the failing key, along the lines of replicaCount: Must be greater than or equal to 1.
Describing nested values
Use nested properties for nested maps. This version covers the whole values.yaml from the chart.
{
"$schema": "https://json-schema.org/draft-07/schema#",
"type": "object",
"required": ["replicaCount", "image"],
"properties": {
"replicaCount": { "type": "integer", "minimum": 1 },
"image": {
"type": "object",
"required": ["repository"],
"properties": {
"repository": { "type": "string", "minLength": 1 },
"tag": { "type": "string" },
"pullPolicy": {
"type": "string",
"enum": ["Always", "IfNotPresent", "Never"]
}
}
},
"service": {
"type": "object",
"properties": {
"type": { "enum": ["ClusterIP", "NodePort", "LoadBalancer"] },
"port": { "type": "integer", "minimum": 1, "maximum": 65535 }
}
},
"env": {
"type": "object",
"additionalProperties": { "type": "string" }
}
}
}The enum keyword catches typos such as pullPolicy: IfNotPrsent. The additionalProperties keyword on env requires every environment variable value to be a string, which matches how the template uses quote.
Common errors
- Strict types and
--set.--set replicaCount=3produces an integer, so it passes.--set image.tag=1.27produces a number, so"type": "string"rejects it. Use--set-string image.tag=1.27, or quote the value in a values file. - Closing the schema too tightly. Adding
"additionalProperties": falseat the top level rejects any key you did not list. This also blocks the values a parent chart passes to a subchart, such asglobal. Addglobaltoproperties, or leave the setting open. - Null values. Setting a key to
nullwith--set key=nullremoves the default. If the schema requires that key, validation then fails. - Subchart schemas. A subchart's own
values.schema.jsonis checked too, so a badredis.*value fails even though your chart's schema never mentions it.
Practical guidance
Write the schema when you write values.yaml, not afterward. Require only the keys that have no sensible default. Constrain with enum, minimum, and pattern where a wrong value causes a runtime failure rather than a render failure. Run helm lint in CI so a schema change that breaks the defaults fails before release.
Exercise
A user runs helm install my-release ./webapp --set service.port=http. Your schema defines service.port as an integer from 1 to 65535. What happens, and at what point?
Show answer
Helm merges the values, then validates them against values.schema.json. http is a string, not an integer, so validation fails and the install stops with an error naming service.port. No template renders and nothing reaches the cluster.
Exercise
Your schema sets "additionalProperties": false at the top level, and you add a Redis subchart. Installs now fail when users set global.imageRegistry. Why, and how do you fix it?
Show answer
global is not listed in the top-level properties, so additionalProperties: false rejects it. Add a global property of type object to the schema, or remove the restriction.