gotchaModeratepending
YAML gotchas — Norway problem and implicit type coercion
Viewed 0 times
Norway problemYAML booleanimplicit typingYAML 1.1version numberyamllint
terminalci-cdlinux
Error Messages
Problem
YAML parses values unexpectedly: 'NO' becomes boolean false, version '1.0' becomes float 1, country code 'NO' becomes false, time '1:30' becomes 90 (seconds). Data corruption happens silently.
Solution
(1) Always quote strings that could be ambiguous: 'NO', '1.0', 'true', 'yes', 'on', 'null'. (2) The Norway problem: country code NO, boolean no, and string 'no' are all different but YAML 1.1 treats unquoted no as false. (3) Version numbers: '3.10' without quotes becomes 3.1 (trailing zero dropped). Always quote: version: '3.10'. (4) Octal numbers: 0777 is octal in YAML 1.1 (becomes 511 decimal). (5) Timestamps: 2024-01-15 without quotes becomes a Date object. (6) Use YAML 1.2 spec (JSON-compatible) when possible — it removes most boolean aliases. (7) Use a YAML linter: yamllint. (8) Consider TOML or JSON for configuration where type safety matters.
Why
YAML 1.1 has extensive implicit typing: it tries to detect booleans, numbers, dates, and nulls from unquoted values. This helpful feature becomes a trap when your data happens to match these patterns.
Revisions (0)
No revisions yet.