# Template analysis

Reading the chart's templates is the core of `helm-schema`. This page says what it collects and how
the conditions around a value shape the schema, so you can predict the output.

## Which values are found

Every `.Values` path the templates use is collected, from:

- **manifests** under `templates/`;
- **helpers**, such as those in `_helpers.tpl`, wherever they are included;
- **YAML fragments** a template loads with `.Files.Get` and a literal path.

`helm-schema` parses the templates, so quoting, pipelines, nested calls and whitespace trimming
(`{{-` and `-}}`) are read as Helm reads them.

A value the templates read becomes a property even when `values.yaml` does not set it. The root is
closed with `additionalProperties: false`, so a key that no template reads is reported, which turns
a misspelled key into a validation error.

## Conditions become structure

What a value must be depends on the conditions around it. This chart renders a
HorizontalPodAutoscaler only when `autoscaling.enabled` is set:

```helm {title="templates/hpa.yaml"}
{{- if .Values.autoscaling.enabled }}
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: guards
spec:
  minReplicas: {{ .Values.autoscaling.minReplicas }}
{{- end }}
```

`helm-schema ./guards --no-k8s-schemas`, which leaves out the Kubernetes types to show only what the
template implies, writes:

```json {title="values.schema.json" collapse=92}
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "additionalProperties": false,
  "allOf": [
    {
      "if": {
        "anyOf": [
          {
            "not": {
              "properties": {
                "autoscaling": {}
              },
              "required": [
                "autoscaling"
              ],
              "type": "object"
            }
          },
          {
            "properties": {
              "autoscaling": {
                "enum": [
                  null
                ]
              }
            },
            "required": [
              "autoscaling"
            ],
            "type": "object"
          }
        ]
      },
      "then": false
    }
  ],
  "properties": {
    "autoscaling": {
      "additionalProperties": {},
      "allOf": [
        {
          "if": {
            "properties": {
              "enabled": {
                "anyOf": [
                  {
                    "const": true
                  },
                  {
                    "not": {
                      "const": 0
                    },
                    "type": "number"
                  },
                  {
                    "minLength": 1,
                    "type": "string"
                  },
                  {
                    "minItems": 1,
                    "type": "array"
                  },
                  {
                    "minProperties": 1,
                    "type": "object"
                  }
                ]
              }
            },
            "required": [
              "enabled"
            ],
            "type": "object"
          },
          "then": {
            "additionalProperties": {},
            "properties": {
              "minReplicas": {
                "type": "integer"
              }
            }
          }
        }
      ],
      "properties": {
        "enabled": {},
        "minReplicas": {}
      },
      "type": "object"
    },
    "global": {}
  },
  "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
    }
  }
}
```

Reading it:

- **The `if` becomes an `if` and `then`.** When `autoscaling.enabled` is true, `minReplicas` must be
  an integer. When it is false, nothing about `minReplicas` is checked, since the template never
  renders it.
- **`autoscaling` must exist.** The template reads `autoscaling.enabled` in every render, so the
  root `allOf` rejects values where `autoscaling` is missing or `null`.

## Truthiness

`autoscaling.enabled` is not `type: boolean`. Helm treats `false`, `0`, `""`, an empty list and an
empty map as false, and everything else as true, so `{{ if .Values.x }}` does not make `x` a
boolean. The condition is an `anyOf` of the forms Helm treats as true:

- `true`;
- a number other than `0`;
- a string, a list or a map that is not empty.

A value only becomes a `boolean` when something types it as one, such as a boolean Kubernetes field
it also renders into.

## Recognized patterns

| Pattern                             | Effect on the schema                                                                    |
| ----------------------------------- | --------------------------------------------------------------------------------------- |
| `if .Values.x`, `with .Values.x`    | `x` is tested for truthiness; what renders inside becomes conditional                   |
| `range .Values.x`                   | `x` is a list or a map; the fields used in the loop apply to its items                  |
| `eq .Values.x "a"`                  | `x` is compared with a literal, which becomes one of its expected values                |
| `default <literal> .Values.x`       | `x` takes the literal's type; see [Values and defaults](https://romnn.github.io/helm-schema/docs/guide/values-and-defaults/) |
| `not`, `or`, `and`                  | combine conditions, as `not`, `anyOf` and `allOf` in the schema                         |

## Where analysis stops

Some values cannot be followed from the templates alone:

- a value read through a key computed at render time;
- a helper whose output depends on data only known when Helm runs;
- a resource whose `apiVersion` cannot be pinned.

`helm-schema` then leaves the value open or reports a
[diagnostic](https://romnn.github.io/helm-schema/docs/reference/diagnostics/), and never invents a type. A
[schema override](https://romnn.github.io/helm-schema/docs/guide/overrides/) fills such a gap by hand.

## Related flags

- **`--no-k8s-schemas`:** only template analysis, without Kubernetes or CRD types. It shows exactly
  what the templates imply, and needs no network.
- **`--exclude-tests`:** leaves out `templates/tests/`.
- **`--infer-required`:** requires values the templates test unconditionally; see
  [Values and defaults](https://romnn.github.io/helm-schema/docs/guide/values-and-defaults/#required-fields).
