Data guide

How to Format and Validate YAML Safely

YAML is readable because whitespace carries structure. A safe formatting workflow checks indentation and duplicate keys without assuming every plain value is a string.

smallpdf.app editorial team Published 9 min read
Illustrated uneven YAML configuration becoming consistently indented YAML with comments and a validation check

Why YAML formatting needs semantic parsing

YAML uses indentation as structure. Moving a line by two spaces can change which mapping or sequence contains it, even when the words remain the same. A formatter therefore needs to parse the document before it can create a consistently indented result.

The YAML Formatter & Validator reports syntax and duplicate-key problems before producing output. It retains comments attached to the parsed document and offers two- or four-space indentation.

Whitespace is YAML syntax. Do not repair indentation by appearance alone. Confirm which mapping or sequence should own every nested value.

Recognize mappings and sequences

A mapping connects keys to values:

project: smallpdf.app
active: true

A sequence uses dash markers:

tools:
  - format
  - compare

A sequence item can itself be a mapping, and mappings can contain further sequences. Consistent indentation makes those relationships visible. Sibling values must align at the same indentation level, while a child value is indented beneath its parent.

Use spaces, not tabs, for indentation

Tabs are not valid indentation in YAML. An editor may render one tab as several spaces, making the source look aligned while the parser sees a different character. Configure the editor to insert spaces when working with YAML, and replace indentation tabs before validation.

Tabs can appear inside quoted scalar content; the restriction concerns indentation used to express structure.

Reject duplicate mapping keys

environment: production
environment: staging

This mapping contains the same key twice. Different YAML consumers may keep the first value, keep the last value, or reject the document. The formatter treats duplicate keys as invalid so the conflict must be resolved explicitly.

Duplicate keys in different nested mappings are not a conflict. The problem occurs when the same mapping contains more than one entry with the same key.

Understand YAML scalar types

Plain YAML values can represent strings, booleans, null, and numbers. Quoting a value can make its string intent clearer:

enabled: true
label: "true"
attempts: 3
code: "003"

The first value is a boolean, while the second is text. The quoted code retains its leading zeros as text. Dates, timestamps, numeric-looking identifiers, and values such as null deserve the same attention when another system depends on their exact type.

Formatting can normalize how parsed values are serialized. Review quotes and scalar styles in configuration files where downstream typing is important.

Preserve comments without treating them as data

Comments explain a document to people but are not values in the data model. The YAML parser retains comments associated with document nodes when formatting. Their spacing or exact presentation may change as the document is serialized.

Check that a comment still appears next to the setting it explains, particularly around sequences, nested mappings, blank lines, and commented-out examples.

Be careful with anchors, aliases, and advanced tags

Anchors and aliases allow one YAML node to be reused elsewhere. Tags can influence how a consumer constructs a value. These features make YAML more expressive than JSON, but they also increase differences between consuming systems.

A syntactically valid document may still use a tag that an application does not support or an alias pattern that the destination interprets differently. Formatting validates the YAML document; it cannot guarantee compatibility with every configuration system.

Format YAML step by step

  1. Load or paste the document. Use a UTF-8 .yaml or .yml file.
  2. Identify indentation. Confirm the intended mapping and sequence hierarchy before changing it.
  3. Choose two or four spaces. Follow the convention of the repository or application that owns the file.
  4. Format and validate. Correct tabs, duplicate keys, unfinished quotes, or invalid collection syntax when reported.
  5. Review semantic values. Check quotes, booleans, null, numbers, identifiers, anchors, aliases, and tags.
  6. Review comments. Make sure each explanation remains attached to the right setting.
  7. Copy or download the result. Test it with the destination application before replacing a working configuration.

YAML validation is not application validation

A YAML parser can confirm that the source forms a valid YAML document. It cannot know whether an application requires a version field, permits a particular option, or accepts a value in that location.

Use the destination’s own configuration validator when one exists. If the data contract is defined as JSON Schema and the YAML can be represented as JSON, conversion and JSON Schema validation may provide another structural check, but YAML-specific tags and aliases may not map directly.

When to use JSON instead

YAML is useful for human-maintained configuration because it supports comments and concise structures. JSON has fewer syntax features and may be a better interchange format for APIs or systems where an unambiguous, widely implemented data model matters.

Use JSON Formatter when the source is JSON. Do not assume that adding braces or changing a file extension converts YAML into JSON.

Final YAML review checklist

  • Indentation uses spaces and expresses the intended hierarchy.
  • Mapping keys are unique within each mapping.
  • Sequences and nested mappings align consistently.
  • Quoted and plain scalars have the intended types.
  • Comments remain beside the settings they explain.
  • Anchors, aliases, and tags are supported by the destination.
  • The application’s own validation passes after formatting.

Use YAML Formatter & Validator to clean up the source. For a related markup workflow, read how to format and validate XML without changing its meaning.