vtjson 2.2.4__tar.gz → 2.2.6__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.
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.4
2
2
  Name: vtjson
3
- Version: 2.2.4
3
+ Version: 2.2.6
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
 
@@ -204,7 +204,7 @@ class SchemaError(Exception):
204
204
  pass
205
205
 
206
206
 
207
- __version__ = "2.2.4"
207
+ __version__ = "2.2.6"
208
208
 
209
209
 
210
210
  @dataclass
@@ -1746,6 +1746,32 @@ class ip_address(compiled_schema):
1746
1746
  return ""
1747
1747
 
1748
1748
 
1749
+ class regex_pattern(compiled_schema):
1750
+ """
1751
+ Matches valid regular expression patterns
1752
+ """
1753
+
1754
+ __name__: str
1755
+
1756
+ def __init__(self) -> None:
1757
+ self.__name__ = "regex_pattern"
1758
+
1759
+ def __validate__(
1760
+ self,
1761
+ obj: object,
1762
+ name: str = "object",
1763
+ strict: bool = True,
1764
+ subs: Mapping[str, object] = {},
1765
+ ) -> str:
1766
+ if not isinstance(obj, str):
1767
+ return _wrong_type_message(obj, name, self.__name__)
1768
+ try:
1769
+ re.compile(obj)
1770
+ except re.error as e:
1771
+ return _wrong_type_message(obj, name, self.__name__, explanation=str(e))
1772
+ return ""
1773
+
1774
+
1749
1775
  class url(compiled_schema):
1750
1776
  """
1751
1777
  Matches valid urls.
@@ -2175,6 +2201,7 @@ class _cond(compiled_schema):
2175
2201
 
2176
2202
  class cond(wrapper):
2177
2203
  """
2204
+ Args is a list of tuples `(if_schema, then_schema)`.
2178
2205
  An object is successively validated against `if_schema1`, `if_schema2`,
2179
2206
  ... until a validation succeeds. When this happens the object should match
2180
2207
  the corresponding `then_schema`. If no `if_schema` succeeds then the
@@ -2243,8 +2270,9 @@ class _fields(compiled_schema):
2243
2270
 
2244
2271
  class fields(wrapper, Generic[StringKeyType]):
2245
2272
  """
2246
- Matches Python objects with attributes `field1, field2, ..., fieldN` whose
2247
- corresponding values should validate against `schema1, schema2, ...,
2273
+ `d` is a dictionary `{"field1": schema1, ...}`.
2274
+ This matches Python objects with attributes `field1, field2, ..., fieldN`
2275
+ whose corresponding values should validate against `schema1, schema2, ...,
2248
2276
  schemaN` respectively.
2249
2277
  """
2250
2278
 
@@ -2306,16 +2334,16 @@ class _filter(compiled_schema):
2306
2334
  obj = self.filter(obj)
2307
2335
  except Exception as e:
2308
2336
  return (
2309
- f"Applying {self.filter_name} to {name} "
2337
+ f"Applying {repr(self.filter_name)} to {name} "
2310
2338
  f"(value: {_c(obj)}) failed: {str(e)}"
2311
2339
  )
2312
2340
  name = f"{self.filter_name}({name})"
2313
- return self.schema.__validate__(obj, name="object", strict=strict, subs=subs)
2341
+ return self.schema.__validate__(obj, name=name, strict=strict, subs=subs)
2314
2342
 
2315
2343
 
2316
2344
  class filter(wrapper):
2317
2345
  """
2318
- Applies `callable` to the object and validates the result with `schema`.
2346
+ Applies `filter` to the object and validates the result with `schema`.
2319
2347
  If the callable throws an exception then validation fails.
2320
2348
  """
2321
2349
 
@@ -2751,6 +2779,9 @@ class protocol(wrapper):
2751
2779
  if not isinstance(dict, bool):
2752
2780
  raise SchemaError("bool flag is not a bool")
2753
2781
 
2782
+ if not hasattr(schema, "__annotations__"):
2783
+ raise SchemaError("schema does not have type annotations")
2784
+
2754
2785
  self.dict = dict
2755
2786
  self.schema = schema
2756
2787
 
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.4
2
2
  Name: vtjson
3
- Version: 2.2.4
3
+ Version: 2.2.6
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
 
@@ -96,6 +96,7 @@ from vtjson import (
96
96
  protocol,
97
97
  quote,
98
98
  regex,
99
+ regex_pattern,
99
100
  safe_cast,
100
101
  set_label,
101
102
  set_name,
@@ -964,7 +965,6 @@ class TestValidation(unittest.TestCase):
964
965
  def test_make_type(self) -> None:
965
966
  schema: object
966
967
  object_: object
967
- global url
968
968
  schema = {"a": 1}
969
969
  t = make_type(schema, "example", debug=True)
970
970
  self.assertTrue(t.__name__ == "example")
@@ -1845,6 +1845,17 @@ class TestValidation(unittest.TestCase):
1845
1845
  validate(schema, object_)
1846
1846
  show(mc)
1847
1847
 
1848
+ def test_regex_pattern(self) -> None:
1849
+ schema: object
1850
+ object_: object
1851
+ schema = regex_pattern
1852
+ with self.assertRaises(ValidationError) as mc:
1853
+ object_ = "(("
1854
+ validate(schema, object_)
1855
+ show(mc)
1856
+ object_ = ".*"
1857
+ validate(schema, object_)
1858
+
1848
1859
  def test_truncation(self) -> None:
1849
1860
  schema: object
1850
1861
  object_: object
@@ -2314,7 +2325,7 @@ class TestValidation(unittest.TestCase):
2314
2325
  if not vtjson.supports_structural:
2315
2326
  with self.assertRaises(SchemaError) as mc_:
2316
2327
  schema = protocol(dummy)
2317
- validate(schema, "a")
2328
+ compile(schema)
2318
2329
  show(mc_)
2319
2330
  return
2320
2331
 
@@ -2324,7 +2335,6 @@ class TestValidation(unittest.TestCase):
2324
2335
 
2325
2336
  with self.assertRaises(SchemaError) as mc_:
2326
2337
  schema = protocol({})
2327
- validate(schema, "a")
2328
2338
  show(mc_)
2329
2339
 
2330
2340
  schema = protocol(dummy)
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes