1---2name: yaml3description: Write valid YAML that parses predictably across languages and versions.4---5
6## Type Coercion Traps
7
8- `yes`, `no`, `on`, `off`, `true`, `false` → boolean; quote if literal string: `"yes"`
9- `NO` (Norway country code) → false in YAML 1.1; always quote country codes
10- `1.0` → float, `1` → int; quote version numbers: `"1.0"`
11- `010` → octal (8) in YAML 1.1; quote or use `0o10` explicitly
12- `null`, `~`, empty value → null; quote if literal: `"null"`, `"~"`
13- `.inf`, `-.inf`, `.nan` → special floats; quote if literal strings
14
15## Indentation
16
17- Spaces only—tabs are forbidden and cause parse errors
18- Consistent indent width required within document—2 spaces conventional
19- Sequence items `-` count as indentation—nested content aligns after the space
20
21## Strings
22
23- Colon followed by space `: ` triggers key-value—quote strings containing `: `
24- `#` starts comment unless quoted—quote strings with `#`
25- Leading/trailing spaces stripped from unquoted strings—quote to preserve
26- Quote strings starting with `@`, `` ` ``, `*`, `&`, `!`, `|`, `>`, `{`, `[`, `%`
27
28## Multiline Strings
29
30- `|` literal block preserves newlines; `>` folded block joins lines with spaces
31- Trailing newline: `|-` and `>-` strip final newline; `|+` and `>+` keep trailing blank lines
32- Indentation of first content line sets the block indent—be consistent
33
34## Structure
35
36- Duplicate keys: YAML spec says last wins, but some parsers error—avoid duplicates
37- Anchors `&name` and aliases `*name` reduce repetition—but aliases can't override anchor values
38- Document separator `---` starts new document; `...` ends document—useful in streams
39- Empty documents between `---` markers are valid but often unintended
40
41## Comments
42
43- `#` only valid at line start or after whitespace—`key:value#comment` has no comment
44- No inline comments after multiline block scalars—comment applies to next line
45- No multi-line comment syntax—each line needs `#`
46
47## Compatibility
48
49- YAML 1.1 vs 1.2: boolean words (`yes`/`no`), octal syntax differ—know which version parser uses
50- JSON is valid YAML 1.2—but YAML features (anchors, multiline) don't round-trip to JSON
51- Some parsers limit nesting depth or file size—test with expected data scale