Mappings combining defined and undefined keys (MapCombined)

Experimental

This feature is in alpha. The API may change on a minor version increment.

When you wish to support arbitrary optional keys in some mappings (i.e. to specify some required keys in the schema, but allow any additional ones on top of that), you use a MapCombined.

See https://github.com/crdoconnor/strictyaml/issues/148#issuecomment-861007657

from strictyaml import Any, Int, MapCombined, Optional, Str, load
from ensure import Ensure

schema = MapCombined(
  {
    "required": Str(),
    Optional("foo"): Int(),
  },
  Str(),
  Any(),
)

Optional is present:

required: Hello World
foo: 42
bar: 42
Ensure(load(yaml_snippet, schema).data).equals(
    {
        "required": "Hello World",
        "foo": 42,
        "bar": "42",
    }
)

Optional is absent:

required: Hello World
bar: 42
Ensure(load(yaml_snippet, schema).data).equals(
    {
        "required": "Hello World",
        "bar": "42",
    }
)

Multiple undefined:

required: Hello World
bar: 42
baz: forty two
Ensure(load(yaml_snippet, schema).data).equals(
    {
        "required": "Hello World",
        "bar": "42",
        "baz": "forty two",
    }
)

Required is absent:

bar: 42
load(yaml_snippet, schema)
strictyaml.exceptions.YAMLValidationError:
while parsing a mapping
required key(s) 'required' not found
  in "<unicode string>", line 1, column 1:
    bar: '42'
     ^ (line: 1)

Undefined of invalid type:

required: Hello World
bar: forty two
load(yaml_snippet, schema)
strictyaml.exceptions.YAMLValidationError:
when expecting an integer
found arbitrary text
  in "<unicode string>", line 2, column 1:
    bar: forty two
    ^ (line: 2)

Invalid key type:

1: Hello World
not_an_integer: 42
load(yaml_snippet, schema)
strictyaml.exceptions.YAMLValidationError:
when expecting an integer
found arbitrary text
  in "<unicode string>", line 2, column 1:
    not_an_integer: '42'
    ^ (line: 2)

Executable specification

Documentation automatically generated from map-combined.story storytests.