8
8
9
9
class RegexVariantName (enum .Enum ):
10
10
default = "default"
11
+ nonunicode = "nonunicode"
11
12
python = "python"
12
13
13
14
@@ -31,23 +32,47 @@ def __init__(self, variant: RegexVariantName) -> None:
31
32
self .variant = variant
32
33
33
34
if self .variant == RegexVariantName .default :
34
- self ._real_implementation = _RegressImplementation ()
35
+ self ._real_implementation = _UnicodeRegressImplementation ()
36
+ elif self .variant == RegexVariantName .nonunicode :
37
+ self ._real_implementation = _NonunicodeRegressImplementation ()
35
38
else :
36
39
self ._real_implementation = _PythonImplementation ()
37
40
38
41
self .check_format = self ._real_implementation .check_format
39
42
self .pattern_keyword = self ._real_implementation .pattern_keyword
40
43
41
44
42
- class _RegressImplementation :
45
+ class _UnicodeRegressImplementation :
46
+ def check_format (self , instance : t .Any ) -> bool :
47
+ if not isinstance (instance , str ):
48
+ return True
49
+ try :
50
+ regress .Regex (instance , flags = "u" )
51
+ except regress .RegressError :
52
+ return False
53
+ return True
54
+
55
+ def pattern_keyword (
56
+ self , validator : t .Any , pattern : str , instance : str , schema : t .Any
57
+ ) -> t .Iterator [jsonschema .ValidationError ]:
58
+ if not validator .is_type (instance , "string" ):
59
+ return
60
+
61
+ try :
62
+ regress_pattern = regress .Regex (pattern , flags = "u" )
63
+ except regress .RegressError :
64
+ yield jsonschema .ValidationError (f"pattern { pattern !r} failed to compile" )
65
+ if not regress_pattern .find (instance ):
66
+ yield jsonschema .ValidationError (f"{ instance !r} does not match { pattern !r} " )
67
+
68
+
69
+ class _NonunicodeRegressImplementation :
43
70
def check_format (self , instance : t .Any ) -> bool :
44
71
if not isinstance (instance , str ):
45
72
return True
46
73
try :
47
74
regress .Regex (instance )
48
- # something is wrong with RegressError getting into the published types
49
- # needs investigation... for now, ignore the error
50
- except regress .RegressError : # type: ignore[attr-defined]
75
+ except regress .RegressError :
51
76
return False
52
77
return True
53
78
@@ -59,7 +84,7 @@ def pattern_keyword(
59
84
60
85
try :
61
86
regress_pattern = regress .Regex (pattern )
62
- except regress .RegressError : # type: ignore[attr-defined]
87
+ except regress .RegressError :
63
88
yield jsonschema .ValidationError (f"pattern { pattern !r} failed to compile" )
64
89
if not regress_pattern .find (instance ):
65
90
yield jsonschema .ValidationError (f"{ instance !r} does not match { pattern !r} " )
0 commit comments