Sequence/list validator (Seq)

Sequences in YAML are denoted by a series of dashes (‘-’) and parsed as a list in python.

Validating sequences of a particular type can be done with the Seq validator, specifying the type.

See also UniqueSeq and FixedSeq for other types of sequence validation.

Example yaml_snippet:

- 1
- 2
- 3
from strictyaml import Seq, Str, Int, load
from ensure import Ensure

Valid Parsed:

Ensure(load(yaml_snippet, Seq(Str()))).equals(["1", "2", "3", ])

Is sequence:

assert load(yaml_snippet, Seq(Str())).is_sequence()

Iterator:

assert [x for x in load(yaml_snippet, Seq(Str()))] == ["1", "2", "3"]

.text is nonsensical:

- â
- 2
- 3
load(yaml_snippet, Seq(Str())).text
builtins.TypeError:YAML(['â', '2', '3']) is a sequence, has no text value.:

Invalid mapping instead:

a: 1
b: 2
c: 3
load(yaml_snippet, Seq(Str()))
strictyaml.exceptions.YAMLValidationError:
when expecting a sequence
  in "<unicode string>", line 1, column 1:
    a: '1'
     ^ (line: 1)
found a mapping
  in "<unicode string>", line 3, column 1:
    c: '3'
    ^ (line: 3)

Invalid nested structure instead:

- 2
- 3
- a:
  - 1
  - 2
load(yaml_snippet, Seq(Str()))
strictyaml.exceptions.YAMLValidationError:
when expecting a str
  in "<unicode string>", line 3, column 1:
    - a:
    ^ (line: 3)
found a mapping
  in "<unicode string>", line 5, column 1:
      - '2'
    ^ (line: 5)

Invalid item in sequence:

- 1.1
- 1.2
load(yaml_snippet, Seq(Int()))
strictyaml.exceptions.YAMLValidationError:
when expecting an integer
found an arbitrary number
  in "<unicode string>", line 1, column 1:
    - '1.1'
     ^ (line: 1)

One invalid item in sequence:

- 1
- 2
- 3.4
load(yaml_snippet, Seq(Int()))
strictyaml.exceptions.YAMLValidationError:
when expecting an integer
found an arbitrary number
  in "<unicode string>", line 3, column 1:
    - '3.4'
    ^ (line: 3)

Executable specification

Page automatically generated from sequence.story.