28
28
DATETIME_RAISES += (ValueError , TypeError )
29
29
30
30
31
- class StrictFormatChecker (FormatChecker ):
32
-
33
- def check (self , instance , format ):
34
- if format not in self .checkers :
35
- raise FormatError (
36
- "Format checker for %r format not found" % (format , ))
37
- return super (StrictFormatChecker , self ).check (
38
- instance , format )
39
-
40
-
41
- oas30_format_checker = StrictFormatChecker ()
42
-
43
-
44
- @oas30_format_checker .checks ('int32' )
45
31
def is_int32 (instance ):
46
32
return isinstance (instance , integer_types )
47
33
48
34
49
- @oas30_format_checker .checks ('int64' )
50
35
def is_int64 (instance ):
51
36
return isinstance (instance , integer_types )
52
37
53
38
54
- @oas30_format_checker .checks ('float' )
55
39
def is_float (instance ):
56
40
return isinstance (instance , float )
57
41
58
42
59
- @oas30_format_checker .checks ('double' )
60
43
def is_double (instance ):
61
44
# float has double precision in Python
62
45
# It's double in CPython and Jython
63
46
return isinstance (instance , float )
64
47
65
48
66
- @oas30_format_checker .checks ('binary' )
67
49
def is_binary (instance ):
68
50
return isinstance (instance , binary_type )
69
51
70
52
71
- @oas30_format_checker .checks ('byte' , raises = (binascii .Error , TypeError ))
72
53
def is_byte (instance ):
73
54
if isinstance (instance , text_type ):
74
55
instance = instance .encode ()
75
56
76
- return b64encode (b64decode (instance )) == instance
57
+ try :
58
+ return b64encode (b64decode (instance )) == instance
59
+ except TypeError :
60
+ return False
77
61
78
62
79
- @oas30_format_checker .checks ("date-time" , raises = DATETIME_RAISES )
80
63
def is_datetime (instance ):
81
- if isinstance (instance , binary_type ):
64
+ if not isinstance (instance , ( binary_type , text_type ) ):
82
65
return False
83
- if not isinstance (instance , text_type ):
84
- return True
85
66
86
67
if DATETIME_HAS_STRICT_RFC3339 :
87
68
return strict_rfc3339 .validate_rfc3339 (instance )
@@ -92,24 +73,63 @@ def is_datetime(instance):
92
73
return True
93
74
94
75
95
- @oas30_format_checker .checks ("date" , raises = ValueError )
96
76
def is_date (instance ):
97
- if isinstance (instance , binary_type ):
77
+ if not isinstance (instance , ( binary_type , text_type ) ):
98
78
return False
99
- if not isinstance (instance , text_type ):
100
- return True
79
+
80
+ if isinstance (instance , binary_type ):
81
+ instance = instance .decode ()
82
+
101
83
return datetime .strptime (instance , "%Y-%m-%d" )
102
84
103
85
104
- @oas30_format_checker .checks ("uuid" , raises = AttributeError )
105
86
def is_uuid (instance ):
106
- if isinstance (instance , binary_type ):
107
- return False
108
- if not isinstance (instance , text_type ):
109
- return True
110
- try :
111
- uuid_obj = UUID (instance )
112
- except ValueError :
87
+ if not isinstance (instance , (binary_type , text_type )):
113
88
return False
114
89
115
- return text_type (uuid_obj ) == instance
90
+ if isinstance (instance , binary_type ):
91
+ instance = instance .decode ()
92
+
93
+ return text_type (UUID (instance )) == instance
94
+
95
+
96
+ def is_password (instance ):
97
+ return True
98
+
99
+
100
+ class OASFormatChecker (FormatChecker ):
101
+
102
+ checkers = {
103
+ 'int32' : (is_int32 , ()),
104
+ 'int64' : (is_int64 , ()),
105
+ 'float' : (is_float , ()),
106
+ 'double' : (is_double , ()),
107
+ 'byte' : (is_byte , (binascii .Error , TypeError )),
108
+ 'binary' : (is_binary , ()),
109
+ 'date' : (is_date , (ValueError , )),
110
+ 'date-time' : (is_datetime , DATETIME_RAISES ),
111
+ 'password' : (is_password , ()),
112
+ # non standard
113
+ 'uuid' : (is_uuid , (AttributeError , ValueError )),
114
+ }
115
+
116
+ def check (self , instance , format ):
117
+ if format not in self .checkers :
118
+ raise FormatError (
119
+ "Format checker for %r format not found" % (format , ))
120
+
121
+ func , raises = self .checkers [format ]
122
+ result , cause = None , None
123
+ try :
124
+ result = func (instance )
125
+ except raises as e :
126
+ cause = e
127
+
128
+ if not result :
129
+ raise FormatError (
130
+ "%r is not a %r" % (instance , format ), cause = cause ,
131
+ )
132
+ return result
133
+
134
+
135
+ oas30_format_checker = OASFormatChecker ()
0 commit comments