不允许的 YAML

StrictYAML 解析了 YAML 规范的一个有意见的子集,该子集拒绝解析标准 YAML 中有效的其他功能。

有关为什么这些功能从 StrictYAML 中剔除的原因,请参见 FAQ。

不允许的 YAML 功能会引发 Disallowed 异常,而语法上无效的 YAML 会引发 ScannerError 或 ComposerError。

每个错误都继承自 YAMLError。

from strictyaml import Map, Int, Any, load
from strictyaml import TagTokenDisallowed, FlowMappingDisallowed, AnchorTokenDisallowed

schema = Map({"x": Map({"a": Any(), "b": Any(), "c": Any()})})

标记令牌

x:
  a: !!str yes
  b: !!str 3.5
  c: !!str yes
load(yaml_snippet, schema, label="disallowed")
strictyaml.exceptions.TagTokenDisallowed:
While scanning
  in "disallowed", line 2, column 11:
      a: !!str yes
              ^ (line: 2)
Found disallowed tag tokens (do not specify types in markup)
  in "disallowed", line 2, column 6:
      a: !!str yes
         ^ (line: 2)

流式序列

[a, b]: [x, y]
load(yaml_snippet, schema, label="disallowed")
strictyaml.exceptions.FlowMappingDisallowed:
While scanning
  in "disallowed", line 1, column 1:
    [a, b]: [x, y]
    ^ (line: 1)
Found ugly disallowed JSONesque flow mapping (surround with ' and ' to make text appear literally)
  in "disallowed", line 1, column 2:
    [a, b]: [x, y]
     ^ (line: 1)

流式映射

x: { a: 1, b: 2, c: 3 }
load(yaml_snippet, schema, label="disallowed")
strictyaml.exceptions.FlowMappingDisallowed:
While scanning
  in "disallowed", line 1, column 4:
    x: { a: 1, b: 2, c: 3 }
       ^ (line: 1)
Found ugly disallowed JSONesque flow mapping (surround with ' and ' to make text appear literally)
  in "disallowed", line 1, column 5:
    x: { a: 1, b: 2, c: 3 }
        ^ (line: 1)

节点锚点和引用

x: 
  a: &node1 3.5
  b: 1
  c: *node1
load(yaml_snippet, schema, label="disallowed")
strictyaml.exceptions.AnchorTokenDisallowed:
While scanning
  in "disallowed", line 2, column 6:
      a: &node1 3.5
         ^ (line: 2)
Found confusing disallowed anchor token (surround with ' and ' to make text appear literally)
  in "disallowed", line 2, column 12:
      a: &node1 3.5
               ^ (line: 2)

语法上无效的 YAML

- invalid
string
load(yaml_snippet, schema, label="disallowed")
strictyaml.ruamel.scanner.ScannerError:
while scanning a simple key
  in "disallowed", line 2, column 1:
    string
    ^ (line: 2)
could not find expected ':'
  in "disallowed", line 3, column 1:

    ^ (line: 3)

混合空格缩进

item:
  two space indent: 2
item two:
    four space indent: 2
load(yaml_snippet, label="disallowed")
strictyaml.exceptions.InconsistentIndentationDisallowed:
While parsing
  in "disallowed", line 4, column 5:
        four space indent: 2
        ^ (line: 4)
Found mapping with indentation inconsistent with previous mapping
  in "disallowed", line 5, column 1:

    ^ (line: 5)

可执行规范

文档从 disallow.story storytests. 自动生成。