# Configuration

A chart can carry its schema settings in a `helm-schema.yaml` at its root, so everyone who generates
its schema gets the same one. It is one way to set them: every key has a flag, and the flags win.

Most charts need no file. It is for a chart whose full schema is too large for Helm or too slow for
Helm to validate, which the `lean` profile helps with.

## The file

```yaml {title="mychart/helm-schema.yaml"}
# The format's version; the only key that is required, and 1 is the only version.
version: 1

# The preset to start from: full, the default, or lean.
profile: lean

# Each emission control is on or off, and overrides the profile's setting.
emission:
  # Checks that apply only when a condition holds, placed at the values root.
  root-anchored-conditionals: off
  # The same, placed inside the object whose values the condition tests.
  local-conditionals: on
  # Rejections of values with which rendering fails, such as a missing object a template reads.
  terminal-clauses: off
  # Checks that depend on which Kubernetes kind a value selects, such as Deployment or
  # StatefulSet; need root-anchored-conditionals or local-conditionals on.
  kind-partitions: off
```

The file is strict: malformed YAML, an unknown key, an unsupported `version` or a combination that
cannot work fails before the chart is analyzed.

## Profiles

A profile is a preset of the four emission controls:

| Profile | Root conditionals | Local conditionals | Terminal clauses | Kind partitions |
| ------- | ----------------- | ------------------ | ---------------- | --------------- |
| `full`  | on                | on                 | on               | on              |
| `lean`  | off               | on                 | off              | off             |

Turning a control off only removes checks, so a schema can only accept more than `full` accepts,
never less. What every profile keeps:

- the constraints that hold without a condition, from the templates and from Kubernetes;
- that a value is present and not `null` where the templates always need it;
- the chart's defaults, and the types they declare;
- how scalars may be spelled, such as quoted numbers.

The `guards` chart from [Template analysis](https://romnn.github.io/helm-schema/docs/guide/template-analysis/) shows
the difference. With `lean`, its schema still checks `minReplicas` when autoscaling is on, a local
conditional, but no longer rejects values without `autoscaling`, a terminal clause.

`kind-partitions: on` needs `root-anchored-conditionals` or `local-conditionals` on, and fails
before analysis otherwise.

## Where it is read from

- **Discovered:** `helm-schema.yaml` at the root of the chart given on the command line, for a
  directory or a `.tgz` archive alike. A file in a dependency is never read.
- **`--config <PATH>`:** reads that file instead, relative to the current directory.
- **`--no-config`:** reads none.

## Which setting wins

From highest to lowest:

1. **An emission flag:** `--root-anchored-conditionals`, `--local-conditionals`,
   `--terminal-clauses` or `--kind-partitions`.
2. **The file's `emission`,** unless `--profile` is given.
3. **The profile,** from `--profile`, else the file's `profile`, else `full`.

`--profile` on the command line drops the file's `emission`, so `--profile lean` always means the
standard `lean`, however the chart's file adjusts it. Emission flags still apply on top.

## Seeing the result

`--print-effective-config` prints every setting and where it came from, and exits without analyzing
the chart. With the file above, but `local-conditionals: off`:

```console
$ helm-schema ./mychart --print-effective-config
version: 1
profile:
  value: lean
  source: ./mychart/helm-schema.yaml
emission:
  root-anchored-conditionals:
    value: off
    source: profile:lean
  local-conditionals:
    value: off
    source: ./mychart/helm-schema.yaml
  terminal-clauses:
    value: off
    source: profile:lean
  kind-partitions:
    value: off
    source: profile:lean
warning: discovered helm-schema config weakens schema emission by disabling: kind-partitions, local-conditionals, root-anchored-conditionals, terminal-clauses
```

The warning is the `DiscoveredConfigWeakensEmission` diagnostic. A configuration file that turns
checks off reports them once per run, so a weaker schema never goes unnoticed. Checks turned
off on the command line are not reported.

## In the output

Every schema records its settings in `x-helm-schema-policy`:

- **`requested-profile`:** the profile asked for.
- **`resolved`:** the four controls as applied.
- **`authoring`:** the [closed root and declared types](https://romnn.github.io/helm-schema/docs/guide/values-and-defaults/#declared-defaults-and-the-closed-root).
- **`policy-fingerprint`:** a hash of all of it, the same for the same settings.
