Debian Codegraph @

hitch/story/non-schema-validation.story

raw
1 Parsing YAML without a schema:
2 docs: howto/without-a-schema
3 based on: strictyaml
4 description: |
5 When using strictyaml you do not have to specify a schema. If
6 you do this, the validator "Any" is used which will accept any
7 mapping and any list and any scalar values (which will always be
8 interpreted as a string, unlike regular YAML).
9
10 This is the recommended approach when rapidly prototyping and the
11 desired schema is fluid.
12
13 When your prototype code is parsing YAML that has a more fixed
14 structure, we recommend that you 'lock it down' with a schema.
15
16 The Any validator can be used inside fixed structures as well.
17 given:
18 setup: |
19 from strictyaml import Str, Any, MapPattern, load
20 from ensure import Ensure
21 yaml_snippet: |
22 a:
23 x: 9
24 y: 8
25 b: 2
26 c: 3
27 variations:
28 Parse without validator:
29 steps:
30 - Run: |
31 Ensure(load(yaml_snippet)).equals({"a": {"x": "9", "y": "8"}, "b": "2", "c": "3"})
32
33 Parse with any validator - equivalent:
34 steps:
35 - Run: |
36 Ensure(load(yaml_snippet, Any())).equals({"a": {"x": "9", "y": "8"}, "b": "2", "c": "3"})
37
38 Fix higher levels of schema:
39 steps:
40 - Run: |
41 Ensure(load(yaml_snippet, MapPattern(Str(), Any()))).equals({"a": {"x": "9", "y": "8"}, "b": "2", "c": "3"})