| 1 |
Revalidate an already validated document: |
| 2 |
docs: howto/revalidation |
| 3 |
based on: strictyaml |
| 4 |
description: | |
| 5 |
When parsing a YAML document you may wish to do more than one validation |
| 6 |
pass over a document. |
| 7 |
|
| 8 |
This is needed when: |
| 9 |
|
| 10 |
* It simplifies your code to apply validation in stages. |
| 11 |
* You want to validate recursively. |
| 12 |
* One part of the document validation depends upon another (this is the example given below). |
| 13 |
given: |
| 14 |
setup: | |
| 15 |
from strictyaml import Str, Int, Map, Seq, Any, load |
| 16 |
from ensure import Ensure |
| 17 |
|
| 18 |
overall_schema = Map({"capitals": Any(), "countries": Seq(Str())}) |
| 19 |
parsed = load(yaml_snippet, overall_schema) |
| 20 |
|
| 21 |
yaml_snippet: | |
| 22 |
capitals: |
| 23 |
UK: 1 |
| 24 |
Germany: 2 |
| 25 |
countries: |
| 26 |
- Germany |
| 27 |
- UK |
| 28 |
variations: |
| 29 |
Reparse mapping: |
| 30 |
steps: |
| 31 |
- Run: |
| 32 |
code: | |
| 33 |
Ensure(parsed.data['capitals']['UK']).equals("1") |
| 34 |
parsed['capitals'].revalidate(Map({capital: Int() for capital in parsed.data['countries']})) |
| 35 |
Ensure(parsed.data['capitals']['UK']).equals(1) |
| 36 |
|
| 37 |
Reparse scalar: |
| 38 |
steps: |
| 39 |
- Run: |
| 40 |
code: | |
| 41 |
Ensure(parsed.data['capitals']['UK']).equals("1") |
| 42 |
parsed['capitals']['UK'].revalidate(Int()) |
| 43 |
|
| 44 |
Ensure(parsed.data['capitals']['UK']).equals(1) |
| 45 |
Ensure(parsed['capitals']['UK'].data).is_an(int) |
| 46 |
|
| 47 |
Parse error: |
| 48 |
given: |
| 49 |
yaml_snippet: | |
| 50 |
capitals: |
| 51 |
UK: 1 |
| 52 |
Germany: 2 |
| 53 |
France: 3 |
| 54 |
countries: |
| 55 |
- Germany |
| 56 |
- UK |
| 57 |
steps: |
| 58 |
- Run: |
| 59 |
code: | |
| 60 |
parsed['capitals'].revalidate(Map({capital: Int() for capital in parsed.data['countries']})) |
| 61 |
raises: |
| 62 |
type: strictyaml.exceptions.YAMLValidationError |
| 63 |
message: |- |
| 64 |
while parsing a mapping |
| 65 |
unexpected key not in schema 'France' |
| 66 |
in "<unicode string>", line 4, column 1: |
| 67 |
France: '3' |
| 68 |
^ (line: 4) |