| 1 |
Mappings combining defined and undefined keys (MapCombined): |
| 2 |
docs: compound/map-combined |
| 3 |
based on: strictyaml |
| 4 |
experimental: yes |
| 5 |
description: | |
| 6 |
When you wish to support arbitrary optional keys in |
| 7 |
some mappings (i.e. to specify some required keys in |
| 8 |
the schema, but allow any additional ones on top of |
| 9 |
that), you use a MapCombined. |
| 10 |
|
| 11 |
See https://github.com/crdoconnor/strictyaml/issues/148#issuecomment-861007657 |
| 12 |
given: |
| 13 |
setup: | |
| 14 |
from strictyaml import Any, Int, MapCombined, Optional, Str, load |
| 15 |
from ensure import Ensure |
| 16 |
|
| 17 |
schema = MapCombined( |
| 18 |
{ |
| 19 |
"required": Str(), |
| 20 |
Optional("foo"): Int(), |
| 21 |
}, |
| 22 |
Str(), |
| 23 |
Any(), |
| 24 |
) |
| 25 |
variations: |
| 26 |
"Optional is present": |
| 27 |
given: |
| 28 |
yaml_snippet: | |
| 29 |
required: Hello World |
| 30 |
foo: 42 |
| 31 |
bar: 42 |
| 32 |
steps: |
| 33 |
- Run: | |
| 34 |
Ensure(load(yaml_snippet, schema).data).equals( |
| 35 |
{ |
| 36 |
"required": "Hello World", |
| 37 |
"foo": 42, |
| 38 |
"bar": "42", |
| 39 |
} |
| 40 |
) |
| 41 |
"Optional is absent": |
| 42 |
given: |
| 43 |
yaml_snippet: | |
| 44 |
required: Hello World |
| 45 |
bar: 42 |
| 46 |
steps: |
| 47 |
- Run: | |
| 48 |
Ensure(load(yaml_snippet, schema).data).equals( |
| 49 |
{ |
| 50 |
"required": "Hello World", |
| 51 |
"bar": "42", |
| 52 |
} |
| 53 |
) |
| 54 |
"Multiple undefined": |
| 55 |
given: |
| 56 |
yaml_snippet: | |
| 57 |
required: Hello World |
| 58 |
bar: 42 |
| 59 |
baz: forty two |
| 60 |
steps: |
| 61 |
- Run: | |
| 62 |
Ensure(load(yaml_snippet, schema).data).equals( |
| 63 |
{ |
| 64 |
"required": "Hello World", |
| 65 |
"bar": "42", |
| 66 |
"baz": "forty two", |
| 67 |
} |
| 68 |
) |
| 69 |
"Required is absent": |
| 70 |
given: |
| 71 |
yaml_snippet: | |
| 72 |
bar: 42 |
| 73 |
steps: |
| 74 |
- Run: |
| 75 |
code: |
| 76 |
load(yaml_snippet, schema) |
| 77 |
raises: |
| 78 |
type: strictyaml.exceptions.YAMLValidationError |
| 79 |
message: |- |
| 80 |
while parsing a mapping |
| 81 |
required key(s) 'required' not found |
| 82 |
in "<unicode string>", line 1, column 1: |
| 83 |
bar: '42' |
| 84 |
^ (line: 1) |
| 85 |
"Undefined of invalid type": |
| 86 |
given: |
| 87 |
setup: | |
| 88 |
from strictyaml import Any, Int, MapCombined, Optional, Str, load |
| 89 |
from ensure import Ensure |
| 90 |
|
| 91 |
schema = MapCombined( |
| 92 |
{ |
| 93 |
"required": Str(), |
| 94 |
}, |
| 95 |
Str(), |
| 96 |
Int(), |
| 97 |
) |
| 98 |
yaml_snippet: | |
| 99 |
required: Hello World |
| 100 |
bar: forty two |
| 101 |
steps: |
| 102 |
- Run: |
| 103 |
code: |
| 104 |
load(yaml_snippet, schema) |
| 105 |
raises: |
| 106 |
type: strictyaml.exceptions.YAMLValidationError |
| 107 |
message: |- |
| 108 |
when expecting an integer |
| 109 |
found arbitrary text |
| 110 |
in "<unicode string>", line 2, column 1: |
| 111 |
bar: forty two |
| 112 |
^ (line: 2) |
| 113 |
"Invalid key type": |
| 114 |
given: |
| 115 |
setup: | |
| 116 |
from strictyaml import Any, Int, MapCombined, Optional, Str, load |
| 117 |
from ensure import Ensure |
| 118 |
|
| 119 |
schema = MapCombined( |
| 120 |
{ |
| 121 |
"1": Str(), |
| 122 |
}, |
| 123 |
Int(), |
| 124 |
Str(), |
| 125 |
) |
| 126 |
yaml_snippet: | |
| 127 |
1: Hello World |
| 128 |
not_an_integer: 42 |
| 129 |
steps: |
| 130 |
- Run: |
| 131 |
code: |
| 132 |
load(yaml_snippet, schema) |
| 133 |
raises: |
| 134 |
type: strictyaml.exceptions.YAMLValidationError |
| 135 |
message: |- |
| 136 |
when expecting an integer |
| 137 |
found arbitrary text |
| 138 |
in "<unicode string>", line 2, column 1: |
| 139 |
not_an_integer: '42' |
| 140 |
^ (line: 2) |