vtjson 2.1.8__tar.gz → 2.2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- vtjson-2.2.0/PKG-INFO +133 -0
- vtjson-2.2.0/README.md +113 -0
- {vtjson-2.1.8 → vtjson-2.2.0}/src/vtjson/vtjson.py +857 -293
- vtjson-2.2.0/src/vtjson.egg-info/PKG-INFO +133 -0
- {vtjson-2.1.8 → vtjson-2.2.0}/tests/test_vtjson.py +29 -30
- vtjson-2.1.8/PKG-INFO +0 -422
- vtjson-2.1.8/README.md +0 -402
- vtjson-2.1.8/src/vtjson.egg-info/PKG-INFO +0 -422
- {vtjson-2.1.8 → vtjson-2.2.0}/AUTHORS +0 -0
- {vtjson-2.1.8 → vtjson-2.2.0}/LICENSE +0 -0
- {vtjson-2.1.8 → vtjson-2.2.0}/pyproject.toml +0 -0
- {vtjson-2.1.8 → vtjson-2.2.0}/setup.cfg +0 -0
- {vtjson-2.1.8 → vtjson-2.2.0}/src/vtjson/__init__.py +0 -0
- {vtjson-2.1.8 → vtjson-2.2.0}/src/vtjson/py.typed +0 -0
- {vtjson-2.1.8 → vtjson-2.2.0}/src/vtjson.egg-info/SOURCES.txt +0 -0
- {vtjson-2.1.8 → vtjson-2.2.0}/src/vtjson.egg-info/dependency_links.txt +0 -0
- {vtjson-2.1.8 → vtjson-2.2.0}/src/vtjson.egg-info/requires.txt +0 -0
- {vtjson-2.1.8 → vtjson-2.2.0}/src/vtjson.egg-info/top_level.txt +0 -0
vtjson-2.2.0/PKG-INFO
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: vtjson
|
|
3
|
+
Version: 2.2.0
|
|
4
|
+
Summary: A lightweight package for validating JSON like Python objects
|
|
5
|
+
Author-email: Michel Van den Bergh <michel.vandenbergh@uhasselt.be>
|
|
6
|
+
Project-URL: Homepage, https://github.com/vdbergh/vtjson
|
|
7
|
+
Project-URL: Bug Tracker, https://github.com/vdbergh/vtjson/issues
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Requires-Python: >=3.7
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
License-File: AUTHORS
|
|
15
|
+
Requires-Dist: dnspython
|
|
16
|
+
Requires-Dist: email_validator
|
|
17
|
+
Requires-Dist: idna
|
|
18
|
+
Requires-Dist: python-magic
|
|
19
|
+
Requires-Dist: typing_extensions
|
|
20
|
+
|
|
21
|
+
# vtjson
|
|
22
|
+
|
|
23
|
+
`vtjson` is an easy to use validation library compatible with Python type annotations.
|
|
24
|
+
|
|
25
|
+
## Introduction
|
|
26
|
+
|
|
27
|
+
Here is a simple schema:
|
|
28
|
+
|
|
29
|
+
```python
|
|
30
|
+
book_schema = {
|
|
31
|
+
"title": str,
|
|
32
|
+
"authors": [str, ...],
|
|
33
|
+
"editor?": str,
|
|
34
|
+
"year": int,
|
|
35
|
+
}
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
The following conventions were used:
|
|
39
|
+
|
|
40
|
+
- As in typescript, a (string) key ending in `?` represents an optional key. The corresponding schema (the item the key points to) will only be used for validation when the key is present in the object that should be validated. A key can also be made optional by wrapping it with `optional_key`.
|
|
41
|
+
- If in a list/tuple the last entry is `...` (ellipsis) it means that the next to last entry will be repeated zero or more times. In this way generic types can be created. For example the schema `[str, ...]` represents a list of strings.
|
|
42
|
+
|
|
43
|
+
Let's try to validate some book objects:
|
|
44
|
+
|
|
45
|
+
```python
|
|
46
|
+
good_book = {
|
|
47
|
+
"title": "Gone with the Wind",
|
|
48
|
+
"authors": ["Margaret Mitchell"],
|
|
49
|
+
"year": 1936,
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
bad_book = {
|
|
53
|
+
"title": "Gone with the Wind",
|
|
54
|
+
"authors": ["Margaret Mitchell"],
|
|
55
|
+
"year": "1936",
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
validate(book_schema, good_book, name="good_book")
|
|
59
|
+
validate(book_schema, bad_book, name="bad_book")
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
As expected `vtjson` throws an exception for the second object:
|
|
63
|
+
|
|
64
|
+
```text
|
|
65
|
+
Traceback (most recent call last):
|
|
66
|
+
...
|
|
67
|
+
raise ValidationError(message)
|
|
68
|
+
vtjson.vtjson.ValidationError: bad_book['year'] (value:'1936') is not of type 'int'
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
We may also rewrite the `book_schema` as a valid Python type annotation.
|
|
72
|
+
|
|
73
|
+
```python
|
|
74
|
+
class book_schema(TypedDict):
|
|
75
|
+
title: str
|
|
76
|
+
authors: list[str]
|
|
77
|
+
editor: NotRequired[str]
|
|
78
|
+
year: int
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
Attempting to validate the bad book would raise the same exception as before.
|
|
82
|
+
|
|
83
|
+
Schemas can of course be more complicated and in particular they can be nested.
|
|
84
|
+
Here is an example that shows more of the features of `vtjson`.
|
|
85
|
+
|
|
86
|
+
```python
|
|
87
|
+
person_schema = {
|
|
88
|
+
"name": regex("[a-zA-Z. ]*"),
|
|
89
|
+
"email?": email,
|
|
90
|
+
"website?": url,
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
book_schema = {
|
|
94
|
+
"title": str,
|
|
95
|
+
"authors": [person_schema, ...],
|
|
96
|
+
"editor?": person_schema,
|
|
97
|
+
"year": intersect(int, ge(1900)),
|
|
98
|
+
}
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
Let's try to validate an object not fitting the schema.
|
|
102
|
+
|
|
103
|
+
```python
|
|
104
|
+
bad_book = {
|
|
105
|
+
"title": "Gone with the Wind",
|
|
106
|
+
"authors": [{"name": "Margaret Mitchell", "email": "margaret@gmailcom"}],
|
|
107
|
+
"year": "1936",
|
|
108
|
+
}
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
```text
|
|
112
|
+
Traceback (most recent call last):
|
|
113
|
+
...
|
|
114
|
+
raise ValidationError(message)
|
|
115
|
+
vtjson.vtjson.ValidationError: bad_book['authors'][0]['email'] (value:'margaret@gmailcom') is not of type 'email': The part after the @-sign is not valid. It should have a period.
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
As before we can rewrite the new `book_schema` as a valid type annotation.
|
|
119
|
+
|
|
120
|
+
```python
|
|
121
|
+
class person_schema(TypedDict):
|
|
122
|
+
name: Annotated[str, regex("[a-zA-Z. ]*")]
|
|
123
|
+
email: NotRequired[Annotated[str, email]]
|
|
124
|
+
website: NotRequired[Annotated[str, url]]
|
|
125
|
+
|
|
126
|
+
class book_schema(TypedDict):
|
|
127
|
+
title: str
|
|
128
|
+
authors: list[person_schema]
|
|
129
|
+
editor: NotRequired[list[person_schema]]
|
|
130
|
+
year: Annotated[int, ge(1900)]
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
For comprehensive documentation about `vtjson` see [https://www.cantate.be/vtjson](https://www.cantate.be/vtjson) (canonical reference) or [https://vtjson.readthedocs.io](https://vtjson.readthedocs.io).
|
vtjson-2.2.0/README.md
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
# vtjson
|
|
2
|
+
|
|
3
|
+
`vtjson` is an easy to use validation library compatible with Python type annotations.
|
|
4
|
+
|
|
5
|
+
## Introduction
|
|
6
|
+
|
|
7
|
+
Here is a simple schema:
|
|
8
|
+
|
|
9
|
+
```python
|
|
10
|
+
book_schema = {
|
|
11
|
+
"title": str,
|
|
12
|
+
"authors": [str, ...],
|
|
13
|
+
"editor?": str,
|
|
14
|
+
"year": int,
|
|
15
|
+
}
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
The following conventions were used:
|
|
19
|
+
|
|
20
|
+
- As in typescript, a (string) key ending in `?` represents an optional key. The corresponding schema (the item the key points to) will only be used for validation when the key is present in the object that should be validated. A key can also be made optional by wrapping it with `optional_key`.
|
|
21
|
+
- If in a list/tuple the last entry is `...` (ellipsis) it means that the next to last entry will be repeated zero or more times. In this way generic types can be created. For example the schema `[str, ...]` represents a list of strings.
|
|
22
|
+
|
|
23
|
+
Let's try to validate some book objects:
|
|
24
|
+
|
|
25
|
+
```python
|
|
26
|
+
good_book = {
|
|
27
|
+
"title": "Gone with the Wind",
|
|
28
|
+
"authors": ["Margaret Mitchell"],
|
|
29
|
+
"year": 1936,
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
bad_book = {
|
|
33
|
+
"title": "Gone with the Wind",
|
|
34
|
+
"authors": ["Margaret Mitchell"],
|
|
35
|
+
"year": "1936",
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
validate(book_schema, good_book, name="good_book")
|
|
39
|
+
validate(book_schema, bad_book, name="bad_book")
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
As expected `vtjson` throws an exception for the second object:
|
|
43
|
+
|
|
44
|
+
```text
|
|
45
|
+
Traceback (most recent call last):
|
|
46
|
+
...
|
|
47
|
+
raise ValidationError(message)
|
|
48
|
+
vtjson.vtjson.ValidationError: bad_book['year'] (value:'1936') is not of type 'int'
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
We may also rewrite the `book_schema` as a valid Python type annotation.
|
|
52
|
+
|
|
53
|
+
```python
|
|
54
|
+
class book_schema(TypedDict):
|
|
55
|
+
title: str
|
|
56
|
+
authors: list[str]
|
|
57
|
+
editor: NotRequired[str]
|
|
58
|
+
year: int
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Attempting to validate the bad book would raise the same exception as before.
|
|
62
|
+
|
|
63
|
+
Schemas can of course be more complicated and in particular they can be nested.
|
|
64
|
+
Here is an example that shows more of the features of `vtjson`.
|
|
65
|
+
|
|
66
|
+
```python
|
|
67
|
+
person_schema = {
|
|
68
|
+
"name": regex("[a-zA-Z. ]*"),
|
|
69
|
+
"email?": email,
|
|
70
|
+
"website?": url,
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
book_schema = {
|
|
74
|
+
"title": str,
|
|
75
|
+
"authors": [person_schema, ...],
|
|
76
|
+
"editor?": person_schema,
|
|
77
|
+
"year": intersect(int, ge(1900)),
|
|
78
|
+
}
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
Let's try to validate an object not fitting the schema.
|
|
82
|
+
|
|
83
|
+
```python
|
|
84
|
+
bad_book = {
|
|
85
|
+
"title": "Gone with the Wind",
|
|
86
|
+
"authors": [{"name": "Margaret Mitchell", "email": "margaret@gmailcom"}],
|
|
87
|
+
"year": "1936",
|
|
88
|
+
}
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
```text
|
|
92
|
+
Traceback (most recent call last):
|
|
93
|
+
...
|
|
94
|
+
raise ValidationError(message)
|
|
95
|
+
vtjson.vtjson.ValidationError: bad_book['authors'][0]['email'] (value:'margaret@gmailcom') is not of type 'email': The part after the @-sign is not valid. It should have a period.
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
As before we can rewrite the new `book_schema` as a valid type annotation.
|
|
99
|
+
|
|
100
|
+
```python
|
|
101
|
+
class person_schema(TypedDict):
|
|
102
|
+
name: Annotated[str, regex("[a-zA-Z. ]*")]
|
|
103
|
+
email: NotRequired[Annotated[str, email]]
|
|
104
|
+
website: NotRequired[Annotated[str, url]]
|
|
105
|
+
|
|
106
|
+
class book_schema(TypedDict):
|
|
107
|
+
title: str
|
|
108
|
+
authors: list[person_schema]
|
|
109
|
+
editor: NotRequired[list[person_schema]]
|
|
110
|
+
year: Annotated[int, ge(1900)]
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
For comprehensive documentation about `vtjson` see [https://www.cantate.be/vtjson](https://www.cantate.be/vtjson) (canonical reference) or [https://vtjson.readthedocs.io](https://vtjson.readthedocs.io).
|