DevToolBoxฟรี
บล็อก

ไวยากรณ์ YAML & การตรวจสอบ: ข้อผิดพลาดที่พบบ่อยและวิธีแก้ไข

9 นาทีในการอ่านโดย 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
บทความนี้มีประโยชน์ไหม?

อัปเดตข่าวสาร

รับเคล็ดลับการพัฒนาและเครื่องมือใหม่ทุกสัปดาห์

ไม่มีสแปม ยกเลิกได้ตลอดเวลา

ลองเครื่องมือที่เกี่ยวข้อง

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

บทความที่เกี่ยวข้อง

JSON เป็น Dart: คู่มือ Model Class สำหรับ Flutter

เรียนรู้การแปลง JSON เป็นคลาส Dart model สำหรับ Flutter fromJson, toJson, null safety และ json_serializable

การตรวจสอบ YAML Docker Compose: 10 ข้อผิดพลาดไวยากรณ์ที่พบบ่อยและวิธีแก้ไข

หยุดเสียเวลากับข้อผิดพลาด YAML Docker Compose เรียนรู้การระบุและแก้ไข 10 ข้อผิดพลาดที่พบบ่อยที่สุด

การตรวจสอบ JSON Schema: ประเภท เครื่องมือ และแนวทางปฏิบัติที่ดี

ทุกอย่างเกี่ยวกับการตรวจสอบ JSON Schema: ตั้งแต่ประเภทพื้นฐานไปจนถึงรูปแบบขั้นสูง ไลบรารีตรวจสอบ และการผสานกับ TypeScript และ API

JSON-YAML Converter Online Guide: Syntax, Tools and Best Practices

Guide complete JSON-YAML conversion. Syntax, js-yaml, PyYAML, yq CLI, Kubernetes, Docker Compose, YAML gotchas and security.