# Standard.to_yaml()

Write the standard as YAML.

Usage

[Source](https://github.com/posit-dev/pointblank/blob/main/pointblank/standard.py#L1737-L1824)

``` python
Standard.to_yaml(path=None)
```

YAML is the natural way to keep a standard: it can sit in version control, so changes to the rules go through review, and other teams can read it without running Python. This method writes the standard under a top-level `standard:` key, with the fields in a fixed order (the name and metadata first, then `applies_to`, the steps, and the thresholds). [`from_yaml()`](../reference/Standard.from_yaml.md#pointblank.Standard.from_yaml) reads the file back into an equal [Standard](../reference/Standard.md#pointblank.Standard), and a [`StandardsRegistry`](../reference/StandardsRegistry.md#pointblank.StandardsRegistry) reads a whole directory of them.

Column selectors and the `applies_to=` table selector are written as `python:` blocks with the code that makes them, which is read back without `allow_python=True` (see [`to_dict()`](../reference/Standard.to_dict.md#pointblank.Standard.to_dict)). Other step arguments must be plain data (strings, numbers, booleans, dates, lists, and dictionaries), so expressions and functions can't be written.

## Parameters

`path: str | Path | None = None`  
An optional path to write the YAML to. Any missing parent directories are created, and an existing file is overwritten. If `None` (the default), nothing is written.

## Returns

`str`  
The YAML text. It's returned whether or not `path=` is given.

## Raises

`ValueError`  
If a step argument can't be represented in YAML (for example, a Polars expression). Nothing is written to `path=` in this case.

## Examples

Let's write a standard for the email columns of the tables in a `marts` schema.

``` python
import pointblank as pb

email_standard = pb.Standard(
    name="email-fields",
    version="1.0.0",
    owner="data-governance",
    steps=[
        pb.Step("col_vals_not_null", columns=pb.matches("email")),
        pb.Step("col_vals_regex", columns=pb.matches("email"), pattern="^[^@]+@[^@]+$"),
    ],
    applies_to=pb.in_schema("marts"),
    thresholds=pb.Thresholds(warning=1, error=0.1),
)

print(email_standard.to_yaml())
```

    standard:
      name: email-fields
      version: 1.0.0
      owner: data-governance
      applies_to:
        python: pb.in_schema('marts')
      steps:
      - col_vals_not_null:
          columns:
            python: pb.matches('email')
      - col_vals_regex:
          columns:
            python: pb.matches('email')
          pattern: ^[^@]+@[^@]+$
      thresholds:
        warning: 1
        error: 0.1

The selectors are written as `python:` blocks. With a path, the file is written too, and reading it back gives a standard equal to the original:

``` python
import tempfile
from pathlib import Path

standard_path = Path(tempfile.mkdtemp()) / "standards" / "email-fields.yaml"

_ = email_standard.to_yaml(standard_path)

pb.Standard.from_yaml(standard_path) == email_standard
```

    True

A directory of such files is a registry of standards, which [`StandardsRegistry`](../reference/StandardsRegistry.md#pointblank.StandardsRegistry) applies all at once.
