Boolean (Bool)

Boolean values can be parsed using a Bool validator.

It case-insensitively interprets "yes", "true", "1", "on" as "True", "y" and their opposites as False.

Different values will trigger a validation error.

When updating boolean values on a YAML object with True or False, the roundtripped string version is set to "yes" and "no".

To have your boolean values updated to a different yes/no string, update with a string instead - e.g. "on" or "off".

Example yaml_snippet:

a: yes
b: true
c: on
d: 1
e: True
f: Y

u: n
v: False
w: 0
x: Off
z: no
from strictyaml import Bool, Str, MapPattern, load
from ensure import Ensure

schema = MapPattern(Str(), Bool())

Parse to YAML object:

Ensure(load(yaml_snippet, schema)).equals({
    "a": True, "b": True, "c": True, "d": True, "e": True, "f": True,
    "u": False, "v": False, "w": False, "x": False, "y": False, "z": False,

YAML object should resolve to True or False:

Ensure(load(yaml_snippet, schema)["w"]).equals(False)

Using .data you can get the actual boolean value parsed:

assert load(yaml_snippet, schema)["a"].data is True

.text returns the text of the boolean YAML:

Ensure(load(yaml_snippet, schema)["y"].text).equals("FALSE")

Update boolean values with string and bool type:

yaml = load(yaml_snippet, schema)
yaml['a'] = 'no'
yaml['b'] = False
yaml['c'] = True
a: no
b: no
c: yes
d: 1
e: True
f: Y

u: n
v: False
w: 0
x: Off
z: no

Cannot cast boolean to string:

str(load(yaml_snippet, schema)["y"])
Cannot cast 'YAML(False)' to str.
Use str( or str(yamlobj.text) instead.

Different uninterpretable values raise validation error:

load('a: yâs', schema)
when expecting a boolean value (one of "yes", "true", "on", "1", "y", "no", "false", "off", "0", "n")
found arbitrary text
  in "<unicode string>", line 1, column 1:
    a: "y\xE2s"
     ^ (line: 1)

Executable specification

Documentation automatically generated from boolean.story storytests.