| 1 |
--- |
| 2 |
title: Why does StrictYAML make you define a schema in Python - a Turing-complete language? |
| 3 |
--- |
| 4 |
|
| 5 |
StrictYAML defines schemas in Python (i.e. Turing-complete) code. For example: |
| 6 |
|
| 7 |
```python |
| 8 |
Map({"name": Str(), "email": Str()}) |
| 9 |
``` |
| 10 |
|
| 11 |
Instead of: |
| 12 |
|
| 13 |
```yaml |
| 14 |
type: map |
| 15 |
mapping: |
| 16 |
"name": |
| 17 |
type: str |
| 18 |
required: yes |
| 19 |
"email": |
| 20 |
type: str |
| 21 |
required: yes |
| 22 |
``` |
| 23 |
|
| 24 |
There are some trade offs here: |
| 25 |
|
| 26 |
Schema definition in a non-Turing-complete language like YAML makes |
| 27 |
the schema programming language independent and gives it more |
| 28 |
potential for being read and understood by non-programmers. |
| 29 |
|
| 30 |
However, schema definition in a non-Turing-complete language also |
| 31 |
restricts and makes certain use cases impossible or awkward. |
| 32 |
|
| 33 |
Some use cases I came across included: |
| 34 |
|
| 35 |
- Being able to import pycountry's country list and restrict "country: " to valid country names. |
| 36 |
- Being able to implement a schema that validated date/time scalar values against the specific date/time parser I wanted. |
| 37 |
- Being able to revalidate sections of the document on a 'second pass' that used new data - e.g. a list in one part of the document is restricted to items which come from another part. |
| 38 |
|
| 39 |
|
| 40 |
## Counterarguments |
| 41 |
|
| 42 |
- <https://github.com/crdoconnor/strictyaml/issues/16> |