| 1 |
Disallowed YAML: |
| 2 |
docs: restrictions/disallowed-yaml |
| 3 |
based on: strictyaml |
| 4 |
description: | |
| 5 |
StrictYAML parses an opinionated subset of the YAML |
| 6 |
specification which refuses to parse features which |
| 7 |
are otherwise valid in standard YAML. |
| 8 |
|
| 9 |
For an explanation as to why these features are stripped |
| 10 |
out of StrictYAML, see the FAQ. |
| 11 |
|
| 12 |
Disallowed YAML features raise Disallowed exceptions |
| 13 |
while syntactically invalid YAML raises ScannerError |
| 14 |
or ComposerError. |
| 15 |
|
| 16 |
Every error inherits from YAMLError. |
| 17 |
given: |
| 18 |
setup: | |
| 19 |
from strictyaml import Map, Int, Any, load |
| 20 |
from strictyaml import TagTokenDisallowed, FlowMappingDisallowed, AnchorTokenDisallowed |
| 21 |
|
| 22 |
schema = Map({"x": Map({"a": Any(), "b": Any(), "c": Any()})}) |
| 23 |
variations: |
| 24 |
Tag tokens: |
| 25 |
given: |
| 26 |
yaml_snippet: | |
| 27 |
x: |
| 28 |
a: !!str yes |
| 29 |
b: !!str 3.5 |
| 30 |
c: !!str yes |
| 31 |
steps: |
| 32 |
- Run: |
| 33 |
code: load(yaml_snippet, schema, label="disallowed") |
| 34 |
raises: |
| 35 |
type: strictyaml.exceptions.TagTokenDisallowed |
| 36 |
message: |- |
| 37 |
While scanning |
| 38 |
in "disallowed", line 2, column 11: |
| 39 |
a: !!str yes |
| 40 |
^ (line: 2) |
| 41 |
Found disallowed tag tokens (do not specify types in markup) |
| 42 |
in "disallowed", line 2, column 6: |
| 43 |
a: !!str yes |
| 44 |
^ (line: 2) |
| 45 |
|
| 46 |
Flow style sequence: |
| 47 |
given: |
| 48 |
yaml_snippet: | |
| 49 |
[a, b]: [x, y] |
| 50 |
steps: |
| 51 |
- Run: |
| 52 |
code: load(yaml_snippet, schema, label="disallowed") |
| 53 |
raises: |
| 54 |
type: strictyaml.exceptions.FlowMappingDisallowed |
| 55 |
message: |- |
| 56 |
While scanning |
| 57 |
in "disallowed", line 1, column 1: |
| 58 |
[a, b]: [x, y] |
| 59 |
^ (line: 1) |
| 60 |
Found ugly disallowed JSONesque flow mapping (surround with ' and ' to make text appear literally) |
| 61 |
in "disallowed", line 1, column 2: |
| 62 |
[a, b]: [x, y] |
| 63 |
^ (line: 1) |
| 64 |
|
| 65 |
Flow style mapping: |
| 66 |
description: | |
| 67 |
To use literally, surround with quotes, e.g. x: '{ a: 1, b: 2, c: 3 }' |
| 68 |
given: |
| 69 |
yaml_snippet: | |
| 70 |
x: { a: 1, b: 2, c: 3 } |
| 71 |
steps: |
| 72 |
- Run: |
| 73 |
code: load(yaml_snippet, schema, label="disallowed") |
| 74 |
raises: |
| 75 |
type: strictyaml.exceptions.FlowMappingDisallowed |
| 76 |
message: |- |
| 77 |
While scanning |
| 78 |
in "disallowed", line 1, column 4: |
| 79 |
x: { a: 1, b: 2, c: 3 } |
| 80 |
^ (line: 1) |
| 81 |
Found ugly disallowed JSONesque flow mapping (surround with ' and ' to make text appear literally) |
| 82 |
in "disallowed", line 1, column 5: |
| 83 |
x: { a: 1, b: 2, c: 3 } |
| 84 |
^ (line: 1) |
| 85 |
|
| 86 |
Node anchors and references: |
| 87 |
description: | |
| 88 |
To use literally, surround with quotes, e.g. x: '{ a: 1, b: 2, c: 3 }' |
| 89 |
given: |
| 90 |
yaml_snippet: | |
| 91 |
x: |
| 92 |
a: &node1 3.5 |
| 93 |
b: 1 |
| 94 |
c: *node1 |
| 95 |
steps: |
| 96 |
- Run: |
| 97 |
code: load(yaml_snippet, schema, label="disallowed") |
| 98 |
raises: |
| 99 |
type: strictyaml.exceptions.AnchorTokenDisallowed |
| 100 |
message: |- |
| 101 |
While scanning |
| 102 |
in "disallowed", line 2, column 6: |
| 103 |
a: &node1 3.5 |
| 104 |
^ (line: 2) |
| 105 |
Found confusing disallowed anchor token (surround with ' and ' to make text appear literally) |
| 106 |
in "disallowed", line 2, column 12: |
| 107 |
a: &node1 3.5 |
| 108 |
^ (line: 2) |
| 109 |
|
| 110 |
|
| 111 |
Syntactically invalid YAML: |
| 112 |
description: | |
| 113 |
To use literally, surround with quotes, e.g. x: '{ a: 1, b: 2, c: 3 }' |
| 114 |
given: |
| 115 |
yaml_snippet: | |
| 116 |
- invalid |
| 117 |
string |
| 118 |
steps: |
| 119 |
- Run: |
| 120 |
code: load(yaml_snippet, schema, label="disallowed") |
| 121 |
raises: |
| 122 |
type: strictyaml.ruamel.scanner.ScannerError |
| 123 |
message: |- |
| 124 |
while scanning a simple key |
| 125 |
in "disallowed", line 2, column 1: |
| 126 |
string |
| 127 |
^ (line: 2) |
| 128 |
could not find expected ':' |
| 129 |
in "disallowed", line 3, column 1: |
| 130 |
|
| 131 |
^ (line: 3) |
| 132 |
|
| 133 |
Mixed space indentation: |
| 134 |
description: | |
| 135 |
You must use consistent spacing |
| 136 |
given: |
| 137 |
yaml_snippet: | |
| 138 |
item: |
| 139 |
two space indent: 2 |
| 140 |
item two: |
| 141 |
four space indent: 2 |
| 142 |
steps: |
| 143 |
- Run: |
| 144 |
code: load(yaml_snippet, label="disallowed") |
| 145 |
raises: |
| 146 |
type: strictyaml.exceptions.InconsistentIndentationDisallowed |
| 147 |
message: "While parsing\n in \"disallowed\", line 4, column 5:\n \ |
| 148 |
\ four space indent: 2\n ^ (line: 4)\nFound mapping with indentation\ |
| 149 |
\ inconsistent with previous mapping\n in \"disallowed\", line 5, column\ |
| 150 |
\ 1:\n \n ^ (line: 5)" |