Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the YAML spec mandate a space after the colon?

The YAML spec clearly states:

Mappings use a colon and space (“: ”) to mark each key: value pair.

So this is legal:

foo: bar

But this, Ain't:

foo:bar

I see many people online that are ranting about the space. I think they have a point. I got burned by it several times myself.

Why is the space mandatory? What was the design consideration behind it?

like image 498
Vitaliy Avatar asked Feb 08 '17 21:02

Vitaliy


People also ask

Does YAML require spaces?

The suggested syntax for YAML files is to use 2 spaces for indentation, but YAML will follow whatever indentation system that the individual file uses. Indentation of two spaces works very well for SLS files given the fact that the data is uniform and not deeply nested.

How do I fix indentation error in YAML?

Fixing errors Implement the fix by adding --- at the top of the YAML file. The second exception occurs on line 8 at the seventh character. The error text states this is an indentation error, and you fix it by adding an additional two spaces before each dash ( - ) character.

What is whitespace in YAML?

YAML uses similar whitespace handling as HTML. In YAML, sequences of spaces, tabs, and carriage return characters are folded into a single space during parse. This wonderful technique makes markup code readable by enabling indentation and word-wrapping without affecting the canonical form of the content.

Does YAML use tabs or spaces?

A YAML file use spaces as indentation, you can use 2 or 4 spaces for indentation, but no tab. In other words, tab indentation is forbidden: Why does YAML forbid tabs? Tabs have been outlawed since they are treated differently by different editors and tools.


1 Answers

It's easy to miss, because that specification uses the bizarre convention of only highlighting the last character of an internal link, but the “: ” in the section you quote is actually a link to another section of the specification which answers your question:

Normally, YAML insists the “:” mapping value indicator be separated from the value by white space. A benefit of this restriction is that the “:” character can be used inside plain scalars, as long as it is not followed by white space. This allows for unquoted URLs and timestamps. It is also a potential source for confusion as “a:1” is a plain scalar and not a key: value pair.

So the motivation is that you can write lists such as this without requiring any quoting:

useful_values:
- 2:30
- http://example.com
- localhost:8080

If the space was optional, this could end up being ambiguous, and interpreted as a set of key-value pairs.

Aside: Here's a snippet of JS to make the link formatting on that document less useless.

document.styleSheets[0].insertRule('a[href^="#"] { color: #00A !important; text-decoration: underline !important; background: none !important; }', 0);
like image 59
IMSoP Avatar answered Sep 17 '22 19:09

IMSoP