# helm-schema

```helm {title="templates/deployment.yaml"}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
spec:
  replicas: {{ .Values.replicaCount }}
```

From that template, `helm-schema` writes:

```json {title="values.schema.json" collapse=19 hl_lines=[9,10,11]}
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "additionalProperties": false,
  "properties": {
    "global": {},
    "replicaCount": {
      "anyOf": [
        {
          "description": "Number of desired pods. This is a pointer to distinguish between explicit zero and not specified. Defaults to 1.",
          "format": "int32",
          "type": "integer"
        },
        {
          "pattern": "^(([+-]_*)?(0|[1-9][0-9_]{0,17}|0[xX][0-9a-fA-F]{1,15}|0[bB][01]{1,62}|0[oO][0-7]{1,20}|0[0-7]{1,20})|[+-]_*0[0-7]{0,8}[89][0-9]{0,8})$",
          "type": "string"
        }
      ]
    }
  },
  "required": [
    "replicaCount"
  ],
  "type": "object",
  "x-helm-schema-generated": true,
  "x-helm-schema-policy": {
    "annotation-format-version": 1,
    "authoring": {
      "declared-types": "assert",
      "root": "closed"
    },
    "modifiers": {
      "overrides": {
        "count": 0,
        "digest": "4f53cda18c2baa0c0354bb5f9a3ecbe5ed12ab4d8e11ba873c2f11161202b945"
      },
      "reference-mode": "fully-inlined"
    },
    "narrowing": [],
    "policy-fingerprint": "8a5e8a392fff0994ab4c850bc117a320c0dfe56736e845b27be81a7404a4384f",
    "policy-vocabulary-version": 2,
    "requested-profile": "full",
    "resolved": {
      "kind-partitions": true,
      "local-conditionals": true,
      "root-anchored-conditionals": true,
      "terminal-clauses": true
    }
  }
}
```

`replicaCount` reaches a Deployment's `spec.replicas`, so the schema gives it that field's `int32`
type and Kubernetes' own description, though `values.yaml` only sets `replicaCount: 1`.

With that schema, Helm rejects a misspelled key before it renders:

```console
$ helm template ./web --set replicaCont=3
Error: values don't meet the specifications of the schema(s) in the following chart(s):
web:
- at '': additional properties 'replicaCont' not allowed
```

## How it works

1. `helm-schema` reads the chart's templates, and those of its dependencies under `charts/`, and
   finds every `.Values` path they use.
2. It follows each value through `if`, `with`, `range` and `default` into the resource field it
   renders, and types it from the Kubernetes or CRD schema of that field.
3. It writes one Draft-07 schema. Saved as `values.schema.json` beside `values.yaml`, it is what
   Helm checks values against on `install`, `upgrade`, `lint` and `template`.

## Get started

In the chart's parent directory:

```sh
helm-schema ./web \
  -o web/values.schema.json
```

Then:

- **[Getting started](https://romnn.github.io/helm-schema/docs/getting-started/):** a worked example, from installing
  to the error Helm prints.
- **[Continuous integration](https://romnn.github.io/helm-schema/docs/ci/):** keep the committed schema in sync with
  the chart.
- **[Schema overrides](https://romnn.github.io/helm-schema/docs/guide/overrides/):** fill in what the templates do not
  say.
