唯一项序列 (UniqueSeq)
UniqueSeq 验证不包含重复值的序列。
示例 yaml_snippet
- A
- B
- C
from strictyaml import UniqueSeq, Str, load, as_document
from ensure import Ensure
schema = UniqueSeq(Str())
有效
Ensure(load(yaml_snippet, schema)).equals(["A", "B", "C", ])
使用一个重复解析会引发异常
- A
- B
- B
load(yaml_snippet, schema)
strictyaml.exceptions.YAMLValidationError:
while parsing a sequence
in "<unicode string>", line 1, column 1:
- A
^ (line: 1)
duplicate found
in "<unicode string>", line 3, column 1:
- B
^ (line: 3)
解析所有重复会引发异常
- 3
- 3
- 3
load(yaml_snippet, schema)
strictyaml.exceptions.YAMLValidationError:
while parsing a sequence
in "<unicode string>", line 1, column 1:
- '3'
^ (line: 1)
duplicate found
in "<unicode string>", line 3, column 1:
- '3'
^ (line: 3)
使用重复序列化会引发异常
as_document(["A", "B", "B"], schema)
strictyaml.exceptions.YAMLSerializationError:
Expecting all unique items, but duplicates were found in '['A', 'B', 'B']'.