YAML Gotchas: Common Pitfalls Every Developer Should Know

Common YAML pitfalls that cause bugs and how to avoid them.

best-practicespitfallsdebugging

YAML Gotchas

YAML's flexibility can lead to unexpected behavior. Here are the most common pitfalls.

1. The Norway Problem

# These are parsed as booleans!
country: NO # false
answer: YES # true
maybe: on # true
disabled: off # false
# Fix: Quote strings
country: "NO"
answer: "YES"

2. Unquoted Strings

# Surprise! These aren't strings:
version: 1.0 # Float: 1.0
port: 8080 # Integer: 8080
time: 12:30 # Sexagesimal: 750 (12*60+30)!
date: 2024-01-15 # Date object
# Fix: Quote when you want strings
version: "1.0"
time: "12:30"

3. Indentation Sensitivity

# This is an object
parent:
child: value
# This is a string "child: value"
parent:
child: value
# Tabs vs spaces (use spaces!)
parent:
child: broken # Tab = error in many parsers

4. Special Characters

# Colon in value needs quoting
url: "https://example.com"
# Hash in value needs quoting
color: "#FF5733"
# Otherwise it's a comment
color: #FF5733 # Everything after # is gone!

Safe YAML Parsing

import yaml from 'js-yaml';

// Use safeLoad to prevent code execution
const data = yaml.load(yamlString, { schema: yaml.JSON_SCHEMA });