固定长度序列 (FixedSeq)

固定长度的序列可以通过一系列不同的(或相同的)类型进行验证。

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

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

等效列表

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

无效列表 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)

无效列表 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)

无效列表 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)

可执行规范

fixed-sequence.story storytests 自动生成的文档。