File: revalidation.md

package info (click to toggle)
python-strictyaml 1.7.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,708 kB
  • sloc: python: 12,836; sh: 48; makefile: 3
file content (107 lines) | stat: -rw-r--r-- 1,762 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
---
title: Revalidate an already validated document
---


When parsing a YAML document you may wish to do more than one validation
pass over a document.

This is needed when:

* It simplifies your code to apply validation in stages.
* You want to validate recursively.
* One part of the document validation depends upon another (this is the example given below).


Example yaml_snippet:

```yaml
capitals:
  UK: 1
  Germany: 2
countries:
  - Germany
  - UK

```


```python
from strictyaml import Str, Int, Map, Seq, Any, load
from ensure import Ensure

overall_schema = Map({"capitals": Any(), "countries": Seq(Str())})
parsed = load(yaml_snippet, overall_schema)

```



Reparse mapping:


```python
Ensure(parsed.data['capitals']['UK']).equals("1")
parsed['capitals'].revalidate(Map({capital: Int() for capital in parsed.data['countries']}))
Ensure(parsed.data['capitals']['UK']).equals(1)

```




Reparse scalar:


```python
Ensure(parsed.data['capitals']['UK']).equals("1")
parsed['capitals']['UK'].revalidate(Int())

Ensure(parsed.data['capitals']['UK']).equals(1)
Ensure(parsed['capitals']['UK'].data).is_an(int)

```




Parse error:

```yaml
capitals:
  UK: 1
  Germany: 2
  France: 3
countries:
  - Germany
  - UK

```


```python
parsed['capitals'].revalidate(Map({capital: Int() for capital in parsed.data['countries']}))

```


```python
strictyaml.exceptions.YAMLValidationError:
while parsing a mapping
unexpected key not in schema 'France'
  in "<unicode string>", line 4, column 1:
      France: '3'
    ^ (line: 4)
```







!!! note "Executable specification"

    Documentation automatically generated from 
    <a href="https://github.com/crdoconnor/strictyaml/blob/master/hitch/story/revalidation.story">revalidation.story
    storytests.