DevToolBoxZA DARMO
Blog

Składnia YAML i walidacja: Częste błędy i jak je naprawić

9 min czytaniaby DevToolBox

YAML (YAML Ain't Markup Language) is the go-to configuration format for Docker, Kubernetes, CI/CD pipelines, and countless other tools. Despite its human-friendly syntax, YAML is surprisingly easy to get wrong. This guide covers the most common YAML errors and how to fix them.

Where YAML Is Used

  • Docker Compose (docker-compose.yml)
  • Kubernetes manifests
  • GitHub Actions / GitLab CI workflows
  • Ansible playbooks
  • Configuration files (Prettier, ESLint, etc.)

Basic YAML Syntax Rules

Indentation

YAML uses spaces (never tabs) for indentation. The number of spaces must be consistent within the same level:

# ✅ Correct (2-space indentation)
server:
  host: localhost
  port: 8080
  database:
    name: myapp
    pool: 10

# ❌ Wrong (tab characters or inconsistent spaces)
server:
	host: localhost    # Tab character - INVALID!
   port: 8080        # 3 spaces - inconsistent!

Key-Value Pairs

Keys and values are separated by a colon followed by a space:

# ✅ Correct
name: John Doe
port: 8080

# ❌ Wrong - missing space after colon
name:John Doe

Multi-line Strings

YAML offers two block scalar styles for multi-line strings:

# | (literal block) - preserves newlines
description: |
  This is line one.
  This is line two.
  Each newline is preserved.

# > (folded block) - folds newlines to spaces
description: >
  This is a long paragraph
  that will be joined into
  a single line.

YAML Data Types

TypeSyntaxExample
Stringplain, "quoted", 'quoted'name: John
Integer42, 0xFF, 0o77port: 8080
Float3.14, .inf, .nanpi: 3.14159
Booleantrue/falsedebug: true
Nullnull, ~, (empty)value: null
DateYYYY-MM-DDdate: 2026-02-10

The "Norway Problem"

In YAML 1.1, `NO` (as in the country code for Norway) is interpreted as boolean `false`. This is because YAML 1.1 treats yes/no, on/off, true/false as booleans. Always quote country codes and similar values:

# ❌ YAML 1.1 interprets these as booleans!
country: NO        # → false
answer: yes        # → true
switch: on         # → true

# ✅ Fix: quote the values
country: "NO"      # → string "NO"
answer: "yes"      # → string "yes"
switch: "on"       # → string "on"

Collections: Lists and Maps

# List (sequence)
fruits:
  - apple
  - banana
  - cherry

# Nested map
database:
  host: localhost
  port: 5432
  credentials:
    user: admin
    password: secret

# Inline / Flow style
fruits: [apple, banana, cherry]
point: {x: 10, y: 20}

Top 10 Common YAML Errors

#ErrorCauseFix
1TabCharacterErrorUsing tab characters for indentationReplace all tabs with spaces (2 or 4)
2Inconsistent indentationMixing 2-space and 4-space indentationUse consistent indentation throughout the file
3Missing space after colonWriting `key:value` instead of `key: value`Always add a space after the colon
4Unquoted special charactersValues containing : or # without quotesQuote strings containing special characters
5Boolean coercionyes/no/on/off/NO interpreted as booleanQuote values that look like booleans: `"yes"`, `"NO"`
6Strings with colons`url: http://example.com` breaks at second colonQuote the entire value: `url: "http://example.com"`
7Duplicate keysSame key appears twice in a mappingRemove duplicate keys (last one wins but this is error-prone)
8Trailing whitespaceInvisible spaces after valuesConfigure editor to trim trailing whitespace
9Wrong list indentationList items not aligned with parent keyAlign `-` with child indentation level
10Missing document separatorMultiple documents without `---` separatorAdd `---` between YAML documents

YAML vs JSON vs TOML

FeatureYAMLJSONTOML
Comments# supportNone# support
Multi-line strings| and >\\n only""" triple quotes
Data typesRich (dates, etc.)Basic (6 types)Rich (dates, etc.)
ReadabilityHighMediumHigh
Machine parsingComplexSimpleMedium
Best forConfig filesAPIs, data exchangeApp config

Validation Best Practices

  • Always use a YAML linter in your CI/CD pipeline
  • Configure your editor to show whitespace characters
  • Use 2-space indentation (most common convention)
  • Quote strings that could be misinterpreted (booleans, numbers with leading zeros)
  • Use YAML schema validation for complex configurations
  • Keep YAML files small and modular when possible

FAQ

Can I use tabs in YAML?

No. YAML strictly forbids tab characters for indentation. You must use spaces. Most editors can be configured to insert spaces when you press the Tab key.

What's the difference between single and double quotes in YAML?

Single quotes preserve literal strings (no escape sequences). Double quotes allow escape sequences like \n, \t, and Unicode escapes \uXXXX. Use single quotes for simple strings and double quotes when you need escape characters.

How do I represent null in YAML?

You can use `null`, `~`, or simply leave the value empty. All three are equivalent: `key: null`, `key: ~`, and `key:` (with nothing after colon+space).

Why does YAML treat "NO" as false?

YAML 1.1 specification treats yes/no, on/off, true/false (case-insensitive) as boolean values. YAML 1.2 fixed this to only recognize true/false. To be safe, always quote strings that could be interpreted as booleans.

𝕏 Twitterin LinkedIn
Czy to było pomocne?

Bądź na bieżąco

Otrzymuj cotygodniowe porady i nowe narzędzia.

Bez spamu. Zrezygnuj kiedy chcesz.

Try These Related Tools

YMLYAML Validator & FormatterY{}JSON ↔ YAML ConverterTYTOML ↔ YAML🐳Docker Compose Generator

Related Articles

JSON do Dart: Kompletny przewodnik po klasach modeli Flutter

Naucz się konwertować JSON na klasy Dart dla Flutter. fromJson, toJson, null safety i json_serializable.

Walidacja YAML Docker Compose: 10 Typowych Błędów Składni i Jak je Naprawić

Przestań tracić czas na błędy YAML Docker Compose. Naucz się rozpoznawać i naprawiać 10 najczęstszych błędów.

Walidacja JSON Schema: Typy, narzędzia i najlepsze praktyki

Wszystko o walidacji JSON Schema: od podstawowych typów po zaawansowane wzorce, biblioteki walidacji i integrację z TypeScript oraz API.

Poradnik Konwertera JSON-YAML Online: Skladnia, Narzedzia i Najlepsze Praktyki

Kompletny poradnik konwersji JSON-YAML. Skladnia, js-yaml, PyYAML, yq CLI, Kubernetes, Docker Compose, pulapki YAML i bezpieczenstwo.