2828 DATETIME_RAISES += (ValueError , TypeError )
2929
3030
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' )
4531def is_int32 (instance ):
4632 return isinstance (instance , integer_types )
4733
4834
49- @oas30_format_checker .checks ('int64' )
5035def is_int64 (instance ):
5136 return isinstance (instance , integer_types )
5237
5338
54- @oas30_format_checker .checks ('float' )
5539def is_float (instance ):
5640 return isinstance (instance , float )
5741
5842
59- @oas30_format_checker .checks ('double' )
6043def is_double (instance ):
6144 # float has double precision in Python
6245 # It's double in CPython and Jython
6346 return isinstance (instance , float )
6447
6548
66- @oas30_format_checker .checks ('binary' )
6749def is_binary (instance ):
6850 return isinstance (instance , binary_type )
6951
7052
71- @oas30_format_checker .checks ('byte' , raises = (binascii .Error , TypeError ))
7253def is_byte (instance ):
7354 if isinstance (instance , text_type ):
7455 instance = instance .encode ()
7556
76- return b64encode (b64decode (instance )) == instance
57+ try :
58+ return b64encode (b64decode (instance )) == instance
59+ except TypeError :
60+ return False
7761
7862
79- @oas30_format_checker .checks ("date-time" , raises = DATETIME_RAISES )
8063def is_datetime (instance ):
81- if isinstance (instance , binary_type ):
64+ if not isinstance (instance , ( binary_type , text_type ) ):
8265 return False
83- if not isinstance (instance , text_type ):
84- return True
8566
8667 if DATETIME_HAS_STRICT_RFC3339 :
8768 return strict_rfc3339 .validate_rfc3339 (instance )
@@ -92,24 +73,63 @@ def is_datetime(instance):
9273 return True
9374
9475
95- @oas30_format_checker .checks ("date" , raises = ValueError )
9676def is_date (instance ):
97- if isinstance (instance , binary_type ):
77+ if not isinstance (instance , ( binary_type , text_type ) ):
9878 return False
99- if not isinstance (instance , text_type ):
100- return True
79+
80+ if isinstance (instance , binary_type ):
81+ instance = instance .decode ()
82+
10183 return datetime .strptime (instance , "%Y-%m-%d" )
10284
10385
104- @oas30_format_checker .checks ("uuid" , raises = AttributeError )
10586def 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 )):
11388 return False
11489
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