Mappings with arbitrary key names (MapPattern)

When you do not wish to let the user define the key names in a mapping and and only specify what type the keys are, use a MapPattern.

When you wish to specify the exact key name, use the 'Map' validator instead.

from strictyaml import MapPattern, Int, Float, Str, Any, Seq, YAMLValidationError, load
from ensure import Ensure

schema = MapPattern(Str(), Int())

Equivalence 1:

â: 1
b: 2
Ensure(load(yaml_snippet, schema)).equals({u"â": 1, "b": 2})

Equivalence 2:

a: 1
c: 3
Ensure(load(yaml_snippet, schema)).equals({"a": 1, "c": 3})

Equivalence 3:

a: 1
Ensure(load(yaml_snippet, schema)).equals({"a": 1, })

With floats and ints:

10.25: 23
20.33: 76
Ensure(load(yaml_snippet, MapPattern(Float(), Int())).data).equals({10.25: 23, 20.33: 76})

With Int and List:

1:
- ABC
2:
- DEF
Ensure(load(yaml_snippet, MapPattern(Int(), Seq(Str()))).data).equals({1: ["ABC"], 2: ["DEF"]})

Invalid 1:

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

Invalid 2:

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

More than the maximum number of keys:

â: 1
b: 2
load(yaml_snippet, MapPattern(Str(), Int(), maximum_keys=1))
strictyaml.exceptions.YAMLValidationError:
while parsing a mapping
  in "<unicode string>", line 1, column 1:
    "\xE2": '1'
     ^ (line: 1)
expected a maximum of 1 key, found 2.
  in "<unicode string>", line 2, column 1:
    b: '2'
    ^ (line: 2)

Fewer than the minimum number of keys:

â: 1
load(yaml_snippet, MapPattern(Str(), Int(), minimum_keys=2))
strictyaml.exceptions.YAMLValidationError:
while parsing a mapping
expected a minimum of 2 keys, found 1.
  in "<unicode string>", line 1, column 1:
    "\xE2": '1'
     ^ (line: 1)

Invalid with non-ascii:

a: 1
b: yâs
c: 3
load(yaml_snippet, schema)
strictyaml.exceptions.YAMLValidationError:
when expecting an integer
found arbitrary text
  in "<unicode string>", line 2, column 1:
    b: "y\xE2s"
    ^ (line: 2)

Executable specification

Documentation automatically generated from mappattern.story storytests.