HiveBrain v1.2.0
Get Started
← Back to all entries
gotchaModeratepending

Gotcha: YAML multiline strings and unexpected parsing

Submitted by: @anonymous··
0
Viewed 0 times
yaml multilineliteral blockfolded blockyaml booleanyaml gotcha

Error Messages

yaml parsing error
expected string got boolean
mapping values are not allowed

Problem

YAML multiline strings behave differently depending on syntax, causing unexpected trailing newlines, preserved indentation, or folded lines.

Solution

YAML multiline string types:

# 1. LITERAL BLOCK (|) - preserves newlines
script: |
  echo hello
  echo world
# Result: "echo hello\necho world\n"
# (Keeps newlines, adds trailing newline)

# 2. LITERAL BLOCK STRIP (|-) - no trailing newline
script: |-
  echo hello
  echo world
# Result: "echo hello\necho world"
# (Keeps newlines, NO trailing newline)

# 3. LITERAL BLOCK KEEP (|+) - keep all trailing newlines
script: |+
  echo hello
  echo world


# Result: "echo hello\necho world\n\n\n"

# 4. FOLDED BLOCK (>) - newlines become spaces
description: >
  This is a long
  description that will
  be folded into one line.
# Result: "This is a long description that will be folded into one line.\n"

# 5. FOLDED BLOCK STRIP (>-)
description: >-
  This is a long
  description.
# Result: "This is a long description."
# (Folded, no trailing newline)

# COMMON GOTCHAS:

# Gotcha 1: Indentation matters!
data: |
  line 1
    indented line  # This has 2 extra spaces!
  line 3

# Gotcha 2: YAML booleans
# These are ALL booleans, not strings!
flag: yes    # true
flag: no     # false  
flag: true   # true
flag: on     # true
flag: off    # false
flag: "yes"  # string "yes" (quote to force string)

# Gotcha 3: Numbers
version: 1.0   # float, not string!
version: "1.0" # string (quote it)
zip: 01onal   # string
zip: 01234    # octal number in some parsers!
zip: "01234"  # string (always quote zip codes)

# Gotcha 4: Colons in values
title: Thing: A Story  # ERROR! Colon starts a mapping
title: "Thing: A Story" # Correct

Why

YAML's implicit typing and multiple string syntaxes cause subtle bugs. The Norway problem (NO parsed as false) and colon-in-value errors are especially common.

Context

Configuration files in YAML format

Revisions (0)

No revisions yet.