Fixed length sequences (FixedSeq)

Sequences of fixed length can be validated with a series of different (or the same) types.

from strictyaml import FixedSeq, Str, Map, Int, Float, YAMLValidationError, load
from ensure import Ensure

schema = FixedSeq([Int(), Map({"x": Str()}), Float()])

Equivalent list:

- 1
- x: 5
- 2.5
Ensure(load(yaml_snippet, schema)).equals([1, {"x": "5"}, 2.5, ])

Invalid list 1:

a: 1
b: 2
c: 3
load(yaml_snippet, schema)
strictyaml.exceptions.YAMLValidationError:
when expecting a sequence of 3 elements
  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 list 2:

- 2
- a
- a:
  - 1
  - 2
load(yaml_snippet, schema)
strictyaml.exceptions.YAMLValidationError:
when expecting a mapping
found arbitrary text
  in "<unicode string>", line 2, column 1:
    - a
    ^ (line: 2)

Invalid list 3:

- 1
- a
load(yaml_snippet, schema)
strictyaml.exceptions.YAMLValidationError:
when expecting a sequence of 3 elements
  in "<unicode string>", line 1, column 1:
    - '1'
     ^ (line: 1)
found a sequence of 2 elements
  in "<unicode string>", line 2, column 1:
    - a
    ^ (line: 2)

Executable specification

Page automatically generated from fixed-sequence.story.