vtjson 2.2.3__py3-none-any.whl → 2.2.5__py3-none-any.whl
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/vtjson.py +78 -49
- {vtjson-2.2.3.dist-info → vtjson-2.2.5.dist-info}/METADATA +3 -2
- vtjson-2.2.5.dist-info/RECORD +9 -0
- {vtjson-2.2.3.dist-info → vtjson-2.2.5.dist-info}/WHEEL +1 -1
- vtjson-2.2.3.dist-info/RECORD +0 -9
- {vtjson-2.2.3.dist-info → vtjson-2.2.5.dist-info/licenses}/AUTHORS +0 -0
- {vtjson-2.2.3.dist-info → vtjson-2.2.5.dist-info/licenses}/LICENSE +0 -0
- {vtjson-2.2.3.dist-info → vtjson-2.2.5.dist-info}/top_level.txt +0 -0
vtjson/vtjson.py
CHANGED
|
@@ -204,7 +204,7 @@ class SchemaError(Exception):
|
|
|
204
204
|
pass
|
|
205
205
|
|
|
206
206
|
|
|
207
|
-
__version__ = "2.2.
|
|
207
|
+
__version__ = "2.2.5"
|
|
208
208
|
|
|
209
209
|
|
|
210
210
|
@dataclass
|
|
@@ -728,29 +728,29 @@ class set_label(wrapper):
|
|
|
728
728
|
)
|
|
729
729
|
|
|
730
730
|
|
|
731
|
-
class
|
|
731
|
+
class _quote(compiled_schema):
|
|
732
|
+
|
|
733
|
+
def __init__(self, schema: object) -> None:
|
|
734
|
+
setattr(self, "__validate__", _const(schema, strict_eq=True).__validate__)
|
|
735
|
+
|
|
736
|
+
|
|
737
|
+
class quote(wrapper):
|
|
732
738
|
"""
|
|
733
739
|
An object matches the schema `quote(schema)` if it is equal to `schema`.
|
|
734
740
|
For example the schema `str` matches strings but the schema `quote(str)`
|
|
735
741
|
matches the object `str`.
|
|
736
742
|
"""
|
|
737
743
|
|
|
738
|
-
schema:
|
|
744
|
+
schema: object
|
|
739
745
|
|
|
740
746
|
def __init__(self, schema: object) -> None:
|
|
741
747
|
"""
|
|
742
748
|
:param schema: the schema to be quoted
|
|
743
749
|
"""
|
|
744
|
-
self.schema =
|
|
750
|
+
self.schema = schema
|
|
745
751
|
|
|
746
|
-
def
|
|
747
|
-
self
|
|
748
|
-
obj: object,
|
|
749
|
-
name: str = "object",
|
|
750
|
-
strict: bool = True,
|
|
751
|
-
subs: Mapping[str, object] = {},
|
|
752
|
-
) -> str:
|
|
753
|
-
return self.schema.__validate__(obj, name=name, strict=strict, subs=subs)
|
|
752
|
+
def __compile__(self, _deferred_compiles: _mapping | None = None) -> _quote:
|
|
753
|
+
return _quote(self.schema)
|
|
754
754
|
|
|
755
755
|
|
|
756
756
|
class _set_name(compiled_schema):
|
|
@@ -806,6 +806,10 @@ class set_name(wrapper):
|
|
|
806
806
|
"""
|
|
807
807
|
if not isinstance(name, str):
|
|
808
808
|
raise SchemaError(f"The name {_c(name)} is not a string")
|
|
809
|
+
if not isinstance(reason, bool):
|
|
810
|
+
raise SchemaError(
|
|
811
|
+
f"The parameter 'reason' (value:{repr(reason)}) is not a boolean"
|
|
812
|
+
)
|
|
809
813
|
self.schema = schema
|
|
810
814
|
self.name = name
|
|
811
815
|
self.reason = reason
|
|
@@ -2171,6 +2175,7 @@ class _cond(compiled_schema):
|
|
|
2171
2175
|
|
|
2172
2176
|
class cond(wrapper):
|
|
2173
2177
|
"""
|
|
2178
|
+
Args is a list of tuples `(if_schema, then_schema)`.
|
|
2174
2179
|
An object is successively validated against `if_schema1`, `if_schema2`,
|
|
2175
2180
|
... until a validation succeeds. When this happens the object should match
|
|
2176
2181
|
the corresponding `then_schema`. If no `if_schema` succeeds then the
|
|
@@ -2201,12 +2206,13 @@ class _fields(compiled_schema):
|
|
|
2201
2206
|
|
|
2202
2207
|
def __init__(
|
|
2203
2208
|
self,
|
|
2204
|
-
d: Mapping[
|
|
2209
|
+
d: Mapping[StringKeyType, object],
|
|
2205
2210
|
_deferred_compiles: _mapping | None = None,
|
|
2206
2211
|
) -> None:
|
|
2207
2212
|
self.d = {}
|
|
2208
2213
|
for k, v in d.items():
|
|
2209
|
-
|
|
2214
|
+
key_ = _canonize_key(k)
|
|
2215
|
+
self.d[key_] = _compile(v, _deferred_compiles=_deferred_compiles)
|
|
2210
2216
|
|
|
2211
2217
|
def __validate__(
|
|
2212
2218
|
self,
|
|
@@ -2236,14 +2242,15 @@ class _fields(compiled_schema):
|
|
|
2236
2242
|
return ""
|
|
2237
2243
|
|
|
2238
2244
|
|
|
2239
|
-
class fields(wrapper):
|
|
2245
|
+
class fields(wrapper, Generic[StringKeyType]):
|
|
2240
2246
|
"""
|
|
2241
|
-
|
|
2242
|
-
|
|
2247
|
+
`d` is a dictionary `{"field1": schema1, ...}`.
|
|
2248
|
+
This matches Python objects with attributes `field1, field2, ..., fieldN`
|
|
2249
|
+
whose corresponding values should validate against `schema1, schema2, ...,
|
|
2243
2250
|
schemaN` respectively.
|
|
2244
2251
|
"""
|
|
2245
2252
|
|
|
2246
|
-
d:
|
|
2253
|
+
d: Mapping[StringKeyType, object]
|
|
2247
2254
|
|
|
2248
2255
|
def __init__(self, d: Mapping[StringKeyType, object]) -> None:
|
|
2249
2256
|
"""
|
|
@@ -2252,7 +2259,6 @@ class fields(wrapper):
|
|
|
2252
2259
|
:raises SchemaError: exception thrown when the schema definition is
|
|
2253
2260
|
found to contain an error
|
|
2254
2261
|
"""
|
|
2255
|
-
self.d = {}
|
|
2256
2262
|
if not isinstance(d, Mapping):
|
|
2257
2263
|
raise SchemaError(f"{repr(d)} is not a Mapping")
|
|
2258
2264
|
for k, v in d.items():
|
|
@@ -2261,8 +2267,7 @@ class fields(wrapper):
|
|
|
2261
2267
|
f"key {repr(k)} in {repr(d)} is not an instance of"
|
|
2262
2268
|
" optional_key and not a string"
|
|
2263
2269
|
)
|
|
2264
|
-
|
|
2265
|
-
self.d[key_] = v
|
|
2270
|
+
self.d = d
|
|
2266
2271
|
|
|
2267
2272
|
def __compile__(self, _deferred_compiles: _mapping | None = None) -> _fields:
|
|
2268
2273
|
return _fields(self.d, _deferred_compiles=_deferred_compiles)
|
|
@@ -2303,16 +2308,16 @@ class _filter(compiled_schema):
|
|
|
2303
2308
|
obj = self.filter(obj)
|
|
2304
2309
|
except Exception as e:
|
|
2305
2310
|
return (
|
|
2306
|
-
f"Applying {self.filter_name} to {name} "
|
|
2311
|
+
f"Applying {repr(self.filter_name)} to {name} "
|
|
2307
2312
|
f"(value: {_c(obj)}) failed: {str(e)}"
|
|
2308
2313
|
)
|
|
2309
2314
|
name = f"{self.filter_name}({name})"
|
|
2310
|
-
return self.schema.__validate__(obj, name=
|
|
2315
|
+
return self.schema.__validate__(obj, name=name, strict=strict, subs=subs)
|
|
2311
2316
|
|
|
2312
2317
|
|
|
2313
2318
|
class filter(wrapper):
|
|
2314
2319
|
"""
|
|
2315
|
-
Applies `
|
|
2320
|
+
Applies `filter` to the object and validates the result with `schema`.
|
|
2316
2321
|
If the callable throws an exception then validation fails.
|
|
2317
2322
|
"""
|
|
2318
2323
|
|
|
@@ -2684,6 +2689,48 @@ class _set(compiled_schema):
|
|
|
2684
2689
|
return str(self.schema_)
|
|
2685
2690
|
|
|
2686
2691
|
|
|
2692
|
+
class _protocol(compiled_schema):
|
|
2693
|
+
|
|
2694
|
+
def __init__(
|
|
2695
|
+
self,
|
|
2696
|
+
schema: object,
|
|
2697
|
+
dict: bool = False,
|
|
2698
|
+
_deferred_compiles: _mapping | None = None,
|
|
2699
|
+
) -> None:
|
|
2700
|
+
type_hints = _get_type_hints(schema)
|
|
2701
|
+
total = True
|
|
2702
|
+
if hasattr(schema, "__total__") and isinstance(schema.__total__, bool):
|
|
2703
|
+
total = schema.__total__
|
|
2704
|
+
type_dict = _to_dict(type_hints, total=total)
|
|
2705
|
+
if hasattr(schema, "__name__") and isinstance(schema.__name__, str):
|
|
2706
|
+
name = schema.__name__
|
|
2707
|
+
else:
|
|
2708
|
+
name = "schema"
|
|
2709
|
+
|
|
2710
|
+
if not dict:
|
|
2711
|
+
setattr(
|
|
2712
|
+
self,
|
|
2713
|
+
"__validate__",
|
|
2714
|
+
_set_name(
|
|
2715
|
+
fields(type_dict),
|
|
2716
|
+
name,
|
|
2717
|
+
reason=True,
|
|
2718
|
+
_deferred_compiles=_deferred_compiles,
|
|
2719
|
+
).__validate__,
|
|
2720
|
+
)
|
|
2721
|
+
else:
|
|
2722
|
+
setattr(
|
|
2723
|
+
self,
|
|
2724
|
+
"__validate__",
|
|
2725
|
+
_set_name(
|
|
2726
|
+
type_dict,
|
|
2727
|
+
name,
|
|
2728
|
+
reason=True,
|
|
2729
|
+
_deferred_compiles=_deferred_compiles,
|
|
2730
|
+
).__validate__,
|
|
2731
|
+
)
|
|
2732
|
+
|
|
2733
|
+
|
|
2687
2734
|
class protocol(wrapper):
|
|
2688
2735
|
"""
|
|
2689
2736
|
An object matches the schema `protocol(schema, dict=False)` if `schema` is
|
|
@@ -2691,9 +2738,8 @@ class protocol(wrapper):
|
|
|
2691
2738
|
which validate the corresponding fields in the object.
|
|
2692
2739
|
"""
|
|
2693
2740
|
|
|
2694
|
-
|
|
2741
|
+
schema: object
|
|
2695
2742
|
dict: bool
|
|
2696
|
-
__name__: str
|
|
2697
2743
|
|
|
2698
2744
|
def __init__(self, schema: object, dict: bool = False):
|
|
2699
2745
|
"""
|
|
@@ -2706,34 +2752,17 @@ class protocol(wrapper):
|
|
|
2706
2752
|
"""
|
|
2707
2753
|
if not isinstance(dict, bool):
|
|
2708
2754
|
raise SchemaError("bool flag is not a bool")
|
|
2709
|
-
|
|
2755
|
+
|
|
2756
|
+
if not hasattr(schema, "__annotations__"):
|
|
2757
|
+
raise SchemaError("schema does not have type annotations")
|
|
2758
|
+
|
|
2710
2759
|
self.dict = dict
|
|
2711
|
-
|
|
2712
|
-
if hasattr(schema, "__total__") and isinstance(schema.__total__, bool):
|
|
2713
|
-
total = schema.__total__
|
|
2714
|
-
self.type_dict = _to_dict(type_hints, total=total)
|
|
2715
|
-
if hasattr(schema, "__name__") and isinstance(schema.__name__, str):
|
|
2716
|
-
self.__name__ = schema.__name__
|
|
2717
|
-
else:
|
|
2718
|
-
self.__name__ = "schema"
|
|
2760
|
+
self.schema = schema
|
|
2719
2761
|
|
|
2720
2762
|
def __compile__(
|
|
2721
2763
|
self, _deferred_compiles: _mapping | None = None
|
|
2722
2764
|
) -> compiled_schema:
|
|
2723
|
-
|
|
2724
|
-
return _set_name(
|
|
2725
|
-
fields(self.type_dict),
|
|
2726
|
-
self.__name__,
|
|
2727
|
-
reason=True,
|
|
2728
|
-
_deferred_compiles=_deferred_compiles,
|
|
2729
|
-
)
|
|
2730
|
-
else:
|
|
2731
|
-
return _set_name(
|
|
2732
|
-
dict(self.type_dict),
|
|
2733
|
-
self.__name__,
|
|
2734
|
-
reason=True,
|
|
2735
|
-
_deferred_compiles=_deferred_compiles,
|
|
2736
|
-
)
|
|
2765
|
+
return _protocol(self.schema, self.dict)
|
|
2737
2766
|
|
|
2738
2767
|
|
|
2739
2768
|
class _Literal(compiled_schema):
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
2
|
Name: vtjson
|
|
3
|
-
Version: 2.2.
|
|
3
|
+
Version: 2.2.5
|
|
4
4
|
Summary: An easy to use validation library compatible with Python type annotations
|
|
5
5
|
Author-email: Michel Van den Bergh <michel.vandenbergh@uhasselt.be>
|
|
6
6
|
Project-URL: Homepage, https://github.com/vdbergh/vtjson
|
|
@@ -17,6 +17,7 @@ Requires-Dist: email_validator
|
|
|
17
17
|
Requires-Dist: idna
|
|
18
18
|
Requires-Dist: python-magic
|
|
19
19
|
Requires-Dist: typing_extensions
|
|
20
|
+
Dynamic: license-file
|
|
20
21
|
|
|
21
22
|
# vtjson
|
|
22
23
|
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
vtjson/__init__.py,sha256=oLX4JH6_R7dYtTiGfBG3pQGR21IArspifdmZilbuGOw,68
|
|
2
|
+
vtjson/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
3
|
+
vtjson/vtjson.py,sha256=AQIoyST4Qfpc6apLbIg4plWEh-z0VlvbAjr03JVr924,87109
|
|
4
|
+
vtjson-2.2.5.dist-info/licenses/AUTHORS,sha256=qmxaXxaIO-YPNHJAZ0dcCrnPCs1x9ocbtMksiy4i80M,21
|
|
5
|
+
vtjson-2.2.5.dist-info/licenses/LICENSE,sha256=n7xW-zX8xBLHzCdqWIMRuMzBD_ACLcNCwio0LEkKt1o,1077
|
|
6
|
+
vtjson-2.2.5.dist-info/METADATA,sha256=4TSessIW50S0gojL08Xhj8k6kze022XbfQrMrIIWUE0,3980
|
|
7
|
+
vtjson-2.2.5.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
8
|
+
vtjson-2.2.5.dist-info/top_level.txt,sha256=9DlSF3l63igcvnYPcj117F2hzOW4Nx0N-JBoW3jjBZM,7
|
|
9
|
+
vtjson-2.2.5.dist-info/RECORD,,
|
vtjson-2.2.3.dist-info/RECORD
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
vtjson/__init__.py,sha256=oLX4JH6_R7dYtTiGfBG3pQGR21IArspifdmZilbuGOw,68
|
|
2
|
-
vtjson/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
3
|
-
vtjson/vtjson.py,sha256=fH8mrDASyEATdvx_ZRNUG5iuMrmLNu-vYw6vrVGXZ1k,86316
|
|
4
|
-
vtjson-2.2.3.dist-info/AUTHORS,sha256=qmxaXxaIO-YPNHJAZ0dcCrnPCs1x9ocbtMksiy4i80M,21
|
|
5
|
-
vtjson-2.2.3.dist-info/LICENSE,sha256=n7xW-zX8xBLHzCdqWIMRuMzBD_ACLcNCwio0LEkKt1o,1077
|
|
6
|
-
vtjson-2.2.3.dist-info/METADATA,sha256=Tljh-yBYXaG0hkFR8n4Yu-gkn_YIhcU5iVvL21JUdAg,3958
|
|
7
|
-
vtjson-2.2.3.dist-info/WHEEL,sha256=PZUExdf71Ui_so67QXpySuHtCi3-J3wvF4ORK6k_S8U,91
|
|
8
|
-
vtjson-2.2.3.dist-info/top_level.txt,sha256=9DlSF3l63igcvnYPcj117F2hzOW4Nx0N-JBoW3jjBZM,7
|
|
9
|
-
vtjson-2.2.3.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|