Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specifying the string-value 'yes' in a YAML property

Tags:

yaml

Consider the following document:

foo:
  bar: Yes

According to the spec, this should be interpreted as a Boolean, not as a String.

However, it seems that this document yields the same interpretation:

foo:
  bar: 'Yes'

If I misunderstand, and 'Yes' (in quotes) should in fact be understood as a String-typed value, where in the specs can I find justification for this?

However, if I do interpret correctly, and these documents are equivalent according to spec, how can I specify a string with the value "Yes" as the value of a YAML property?

like image 435
Tomas Aschan Avatar asked Dec 06 '18 09:12

Tomas Aschan


People also ask

How do you define a string in YAML?

In general, you don't need quotes. Use quotes to force a string, e.g. if your key or value is 10 but you want it to return a String and not a Fixnum, write '10' or "10" . Use quotes if your value includes special characters, (e.g. : , { , } , [ , ] , , , & , * , # , ? , | , - , < , > , = , ! , % , @ , \ ).

How do I specify a boolean in YAML?

We use the standard numeric literals for integers and floating-point numbers. Boolean values can be represented in YAML using the keywords like true and false or Yes and No .

Do strings need to be quoted in YAML?

Quotes are required when the string contains special or reserved characters. The double-quoted style provides a way to express arbitrary strings, by using \ to escape characters and sequences. For instance, it is very useful when you need to embed a \n or a Unicode character in a string.

What does |- mean in YAML?

All YAML files (regardless of their association with Ansible or not) can optionally begin with --- and end with ... . This is part of the YAML format and indicates the start and end of a document. All members of a list are lines beginning at the same indentation level starting with a "- " (a dash and a space):


1 Answers

It depends ;-)

In YAML 1.1 the bool type is defined as following:

A Boolean represents a true/false value. Booleans are formatted as English words (“true”/“false”, “yes”/“no” or “on”/“off”) for readability and may be abbreviated as a single character “y”/“n” or “Y”/“N”.

In YAML 1.2 the bool type is defined as following:

Booleans: [ true, True, false, FALSE ]

Assigning the value Yes to a key is done via quotes:

foo: 'Yes'
bar: "Yes"

Assigning a boolean and to be compatible with future versions of YAML parsers should be done with

foo: false
bar: True

You can play around yourself with YAML syntax at the https://yamlvalidator.com/

like image 177
JGK Avatar answered Oct 15 '22 16:10

JGK