NTV.json_ntv.ntv_validate
@author: Philippe@loco-labs.io
The ntv_validate
module is part of the NTV.json_ntv
package (specification document).
It contains the Validator
class.
Function in Validator class are Datatype.validate overloading for each subclass.
For more information, see the user guide or the github repository.
1# -*- coding: utf-8 -*- 2""" 3@author: Philippe@loco-labs.io 4 5The `ntv_validate` module is part of the `NTV.json_ntv` package ([specification document]( 6https://loco-philippe.github.io/ES/JSON%20semantic%20format%20(JSON-NTV).htm)). 7 8It contains the `Validator` class. 9 10Function in Validator class are Datatype.validate overloading for each subclass. 11 12For more information, see the 13[user guide](https://loco-philippe.github.io/NTV/documentation/user_guide.html) 14or the [github repository](https://github.com/loco-philippe/NTV). 15""" 16import datetime 17import re 18 19_dur_s = "([0-9]+S)" 20_dur_n = '(' + '([0-9]+M)' + _dur_s + '?)' 21_dur_h = '(' + '([0-9]+H)' + _dur_n + '?)' 22_dur_time = '(T(' + _dur_h + '|' + _dur_n + '|' + _dur_s + '))' 23_dur_d = "([0-9]+D)" 24_dur_m = '(' + '([0-9]+M)' + _dur_d + '?)' 25_dur_y = '(' + '([0-9]+Y)' + _dur_m + '?)' 26_dur_date = '((' + _dur_d + '|'+_dur_m + '|' + \ 27 _dur_y + ')(' + _dur_time + ')?)' 28_dur_week = '([0-9]+W)' 29DURATION = re.compile('P('+_dur_date+'|'+_dur_time+'|'+_dur_week+')') 30GEOJSON = {'Point': 'coordinates', 'LineString': 'coordinates', 31 'Polygon': 'coordinates', 'MultiPoint': 'coordinates', 32 'MultiLineString': 'coordinates', 'MultiPolygon': 'coordinates', 33 'GeometryCollection': 'geometries', 'Feature': 'geometry', 34 'FeatureCollection': 'features'} 35OLC = re.compile( 36 '([2-90CFGHJMPQRVWX]{2}){4}\+([2-9CFGHJMPQRVWX]{2}[2-9CFGHJMPQRVWX]*)?') 37URI = re.compile('^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?') 38UUID = re.compile('[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}', 39 flags=re.IGNORECASE) 40_dot_atom = r'(\s*[a-zA-Z0-9!#\$%&\*\+-/=\^_`{}\|~.]+)' 41_quoted_string = r'(\s*"[\s*[!#-~]*]*\s*"\s*)' 42_domain_literal = r'(\s*\[(\s*[!-Z^-~]+)*\s*\]\s*)' 43_addr_spec = '((' + _dot_atom + '|' + _quoted_string + \ 44 ')@(' + _dot_atom + '|' + _domain_literal + '))' 45_mailbox = r'((.*\s*\<' + _addr_spec + r'\>\s*)|' + _addr_spec + ')' 46ADDRESS = re.compile( 47 _mailbox + '|(.*:(' + _mailbox + '(,' + _mailbox + r')*)?;\s*)') # without CFWS 48HOSTNAME = re.compile(r'[-a-zA-Z_]{1,63}(\.[-a-zA-Z_]{1,63})*') 49IPV4 = re.compile( 50 '(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])') 51_path_absolute = r'(([a-zA-Z]:)?((/([-a-z0-9_~!&,;=:@\.\$\'\(\)\*\+]|(%[a-f0-9]{2}))*)*))' 52FILE = re.compile(r'/*[-a-z0-9_~!&,;=:@\.\$\'\(\)\*\+]*' + _path_absolute) 53 54 55class Validator: 56 '''Validator class contains static methods. 57 Each method is associated to a global Datatype (name before the _) and 58 return a boolean (type conformity).''' 59 60 def json_valid(val): 61 return isinstance(val, (float, int, str, dict, list, bool)) or val is None 62 63 def number_valid(val): 64 return isinstance(val, (float, int)) 65 66 def boolean_valid(val): 67 return isinstance(val, bool) 68 69 def null_valid(val): 70 return val is None 71 72 def string_valid(val): 73 return isinstance(val, str) 74 75 def array_valid(val): 76 return isinstance(val, list) 77 78 def object_valid(val): 79 return isinstance(val, dict) 80 81 def int_valid(val): 82 return isinstance(val, int) 83 84 def int8_valid(val): 85 return isinstance(val, int) and -128 <= val <= 127 86 87 def int16_valid(val): 88 return isinstance(val, int) and -32768 <= val <= 32767 89 90 def int32_valid(val): 91 return isinstance(val, int) and -2147483648 <= val <= 2147483647 92 93 def int64_valid(val): 94 return isinstance(val, int) and -2 ^ 63 <= val <= 2 ^ 63 95 96 def uint8_valid(val): 97 return isinstance(val, int) and 0 <= val <= 255 98 99 def uint16_valid(val): 100 return isinstance(val, int) and 0 <= val <= 65535 101 102 def uint32_valid(val): 103 return isinstance(val, int) and 0 <= val <= 4294967295 104 105 def uint64_valid(val): 106 return isinstance(val, int) and 0 <= val <= 2 ^ 64 - 1 107 108 def float_valid(val): 109 return isinstance(val, float) 110 111 def float16_valid(val): 112 return isinstance(val, float) and abs(val) <= 65500 113 114 def float32_valid(val): 115 return isinstance(val, float) and abs(val) <= 3.4028237E38 116 117 def float64_valid(val): 118 return isinstance(val, float) 119 120 def decimal64_valid(val): 121 return isinstance(val, float) 122 123 def bit_valid(val): 124 if not isinstance(val, str): 125 return False 126 return val in ['0', '1'] 127 128 def binary_valid(val): 129 if not isinstance(val, str): 130 return False 131 for char in val: 132 if not char in ['0', '1']: 133 return False 134 return True 135 136 def base64_valid(val): 137 if not isinstance(val, str): 138 return False 139 for car in val: 140 if (not 'a' <= car <= 'z' and not 'A' <= car <= 'Z' and 141 not '0' <= car <= '9' and not car in ['-', '_', '=']): 142 return False 143 return True 144 145 def base32_valid(val): 146 if not isinstance(val, str): 147 return False 148 for car in val: 149 if not 'A' <= car <= 'Z' and not '1' < car < '8' and not car == '=': 150 return False 151 return True 152 153 def base16_valid(val): 154 if not isinstance(val, str): 155 return False 156 for car in val: 157 if not '0' <= car <= '9' and not 'A' <= car <= 'F': 158 return False 159 return True 160 161 def year_valid(val): 162 return isinstance(val, int) and 0 <= val 163 164 def month_valid(val): 165 return isinstance(val, int) and 0 < val < 13 166 167 def yearmonth_valid(val): 168 if not isinstance(val, str): 169 return False 170 y_m = val.split('-', maxsplit=1) 171 return Validator.year_valid(int(y_m[0])) and Validator.month_valid(int(y_m[1])) 172 173 def week_valid(val): 174 return isinstance(val, int) and 0 < val < 54 175 176 def day_valid(val): 177 return isinstance(val, int) and 0 < val < 32 178 179 def wday_valid(val): 180 return isinstance(val, int) and 0 < val < 8 181 182 def yday_valid(val): 183 return isinstance(val, int) and 0 < val < 367 184 185 def hour_valid(val): 186 return isinstance(val, int) and 0 <= val < 13 187 188 def minute_valid(val): 189 return isinstance(val, int) and 0 <= val < 60 190 191 def second_valid(val): 192 return isinstance(val, int) and 0 <= val < 60 193 194 def dat_valid(val): 195 return (Validator.date_valid(val) or Validator.time_valid(val) or 196 Validator.datetime_valid(val) or Validator.timetz_valid(val) or 197 Validator.datetimetz_valid(val)) 198 199 def date_valid(val): 200 try: 201 datetime.date.fromisoformat(val) 202 except ValueError: 203 return False 204 return True 205 206 def time_valid(val): 207 try: 208 tim = datetime.time.fromisoformat(val) 209 except ValueError: 210 return False 211 return True if not tim.tzinfo else False 212 213 def timetz_valid(val): 214 try: 215 tim = datetime.time.fromisoformat(val) 216 except ValueError: 217 return False 218 return True if tim.tzinfo else False 219 220 def datetime_valid(val): 221 try: 222 tim = datetime.datetime.fromisoformat(val) 223 except ValueError: 224 return False 225 return True if not tim.tzinfo else False 226 227 def datetimetz_valid(val): 228 try: 229 tim = datetime.datetime.fromisoformat(val) 230 except ValueError: 231 return False 232 return True if tim.tzinfo else False 233 234 def duration_valid(val): 235 if not isinstance(val, str): 236 return False 237 return DURATION.fullmatch(val) is not None 238 239 def period_valid(val): 240 if not isinstance(val, str): 241 return False 242 period = val.split('/', maxsplit=1) 243 for per in period: 244 if not (Validator.datetime_valid(per) or 245 Validator.datetimetz_valid(per) or 246 Validator.duration_valid(per)): 247 return False 248 if period[0][0] == 'P' and period[1][0] == 'P': 249 return False 250 return True 251 252 def timearray_valid(val): 253 return (isinstance(val, list) and len(val) == 2 and 254 Validator.dat_valid(val[0]) and Validator.dat_valid(val[1])) 255 256 def point_valid(val): 257 return (isinstance(val, list) and len(val) == 2 and 258 isinstance(val[0], (int, float)) and -180 <= val[0] <= 180 and 259 isinstance(val[1], (int, float)) and -180 <= val[1] <= 180) 260 261 def pointstr_valid(val): 262 if not isinstance(val, str): 263 return False 264 coord = val.split(',', maxsplit=1) 265 if len(coord) != 2: 266 return False 267 try: 268 point = [float(coord[0]), float(coord[1])] 269 except ValueError: 270 return False 271 return Validator.point_valid(point) 272 273 def pointobj_valid(val): 274 if not (isinstance(val, dict) and 'lon' in val and 'lat' in val): 275 return False 276 return Validator.point_valid([val['lon'], val['lat']]) 277 278 def multipoint_valid(val): 279 if not isinstance(val, list): 280 return False 281 for point in val: 282 if not Validator.point_valid(point): 283 return False 284 return True 285 286 def line_valid(val): 287 return Validator.multipoint_valid(val) 288 289 def multiline_valid(val): 290 if not isinstance(val, list): 291 return False 292 for line in val: 293 if not Validator.multipoint_valid(line): 294 return False 295 return True 296 297 def polygon_valid(val): 298 return Validator.multiline_valid(val) 299 300 def multipolygon_valid(val): 301 if not isinstance(val, list): 302 return False 303 for poly in val: 304 if not Validator.multiline_valid(poly): 305 return False 306 return True 307 308 def geometry_valid(val): 309 return (Validator.point_valid(val) or Validator.line_valid(val) or 310 Validator.polygon_valid(val)) 311 312 def multigeometry_valid(val): 313 if not isinstance(val, list): 314 return False 315 for geo in val: 316 if not Validator.geometry_valid(geo): 317 return False 318 return True 319 320 def box_valid(val): 321 if not (isinstance(val, list) and len(val) == 4): 322 return False 323 for coor in val: 324 if not (isinstance(coor, (int, float)) and -90 <= coor <= 90): 325 return False 326 return True 327 328 def geojson_valid(val): 329 if not (isinstance(val, dict) and 'type' in val): 330 return False 331 return val['type'] in GEOJSON and GEOJSON[val['type']] in val 332 333 def codeolc_valid(val): 334 if not isinstance(val, str): 335 return False 336 return OLC.fullmatch(val) is not None 337 338 def loc_valid(val): 339 return (Validator.point_valid(val) or Validator.pointstr_valid(val) or 340 Validator.pointobj_valid(val) or Validator.line_valid(val) or 341 Validator.polygon_valid(val) or Validator.multipolygon_valid(val) or 342 Validator.box_valid(val) or Validator.geojson_valid(val) or 343 Validator.codeolc_valid(val)) 344 345 def uri_valid(val): 346 if not isinstance(val, str): 347 return False 348 return URI.fullmatch(val) is not None 349 350 def uriref_valid(val): 351 if not isinstance(val, str): 352 return False 353 return URI.fullmatch(val) is not None 354 355 def iri_valid(val): 356 if not isinstance(val, str): 357 return False 358 return URI.fullmatch(val) is not None 359 360 def iriref_valid(val): 361 if not isinstance(val, str): 362 return False 363 return URI.fullmatch(val) is not None 364 365 def uritem_valid(val): 366 if not isinstance(val, str): 367 return False 368 return URI.fullmatch(val) is not None 369 370 def uuid_valid(val): 371 if not isinstance(val, str): 372 return False 373 return UUID.fullmatch(val) is not None 374 375 def email_valid(val): 376 if not isinstance(val, str): 377 return False 378 return ADDRESS.fullmatch(val) is not None 379 380 def hostname_valid(val): 381 if not isinstance(val, str) or len(val) > 253: 382 return False 383 return HOSTNAME.fullmatch(val) is not None 384 385 def jpointer_valid(val): 386 if not isinstance(val, str) or (len(val) > 0 and val[0] != '/'): 387 return False 388 return True 389 390 def ipv4_valid(val): 391 if not isinstance(val, str): 392 return False 393 return IPV4.fullmatch(val) is not None 394 395 def file_valid(val): 396 if not isinstance(val, str): 397 return False 398 return FILE.fullmatch(val) is not None 399 400 def ipv6_valid(val): 401 pass 402 403 def idnemail_valid(val): 404 pass 405 406 def idnhostname_valid(val): 407 pass 408 409 def rjpointer_valid(val): 410 pass 411 412 def regex_valid(val): 413 pass 414 415 def row_valid(val): 416 pass 417 418 def tab_valid(val): 419 pass 420 421 def field_valid(val): 422 pass 423 424 def ntv_valid(val): 425 pass 426 427 def sch_valid(val): 428 pass 429 430 431class ValidateError(Exception): 432 '''Validator exception''' 433 434 435""" 436_IPv6_1_R_H16 = '(([0-9a-f]{1,4})\:){6,6}((([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3,3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|([0-9a-f]{1,4})\:([0-9a-f]{1,4}))' 437_IPV6_2_R_H16 = '\:\:(([0-9a-f]{1,4})\:){5,5}((([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3,3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|([0-9a-f]{1,4})\:([0-9a-f]{1,4}))' 438_IPV6_3_L_H16 = '([0-9a-f]{1,4})?\:\:(([0-9a-f]{1,4})\:){4,4}((([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3,3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|([0-9a-f]{1,4})\:([0-9a-f]{1,4}))' 439_IPV6_4_L_H16_REPEAT = '((([0-9a-f]{1,4})\:)?([0-9a-f]{1,4}))?\:\:(([0-9a-f]{1,4})\:){3,3}((([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3,3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|([0-9a-f]{1,4})\:([0-9a-f]{1,4}))' 440_IPV6_5_L_H16_REPEAT = '((([0-9a-f]{1,4})\:){0,2}([0-9a-f]{1,4}))?\:\:(([0-9a-f]{1,4})\:){2,2}((([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3,3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|([0-9a-f]{1,4})\:([0-9a-f]{1,4}))' 441_IPV6_6_L_H16_REPEAT = '((([0-9a-f]{1,4})\:){0,3}([0-9a-f]{1,4}))?\:\:([0-9a-f]{1,4})\:((([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3,3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|([0-9a-f]{1,4})\:([0-9a-f]{1,4}))' 442_IPV6_7_L_H16_REPEAT = '((([0-9a-f]{1,4})\:){0,4}([0-9a-f]{1,4}))?\:\:((([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3,3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|([0-9a-f]{1,4})\:([0-9a-f]{1,4}))' 443_IPV6_8_L_H16_REPEAT = '((([0-9a-f]{1,4})\:){0,5}([0-9a-f]{1,4}))?\:\:([0-9a-f]{1,4})' 444_IPV6_9_L_H16_REPEAT = '((([0-9a-f]{1,4})\:){0,6}([0-9a-f]{1,4}))?\:\:' 445_ipv6 = '(' + _IPv6_1_R_H16 + '|' + _IPV6_2_R_H16 + '|' + _IPV6_3_L_H16 + '|' + \ 446 _IPV6_4_L_H16_REPEAT + '|' + _IPV6_5_L_H16_REPEAT + '|' + _IPV6_6_L_H16_REPEAT + \ 447 '|' + _IPV6_7_L_H16_REPEAT + '|' + _IPV6_8_L_H16_REPEAT + '|' + _IPV6_9_L_H16_REPEAT + ')' 448_scheme = '([a-z][a-z0-9\+\-\.]*)' 449_userinfo = '(((\%[0-9a-f][0-9a-f]|[a-z0-9\-\.\_\~]|[\!\$\&\'\(\)\*\+\,\;\=]|\:)*)\@)' 450_fragment = '(#([a-z0-9\-\.\_\~\!\$\&\'\(\)\*\+\,\;\=\:\@\/\?]|(%[a-f0-9]{2,2}))*)' 451_query = '(\?([a-z0-9\-\.\_\~\!\$\&\'\(\)\*\+\,\;\=\:\@\/\?]|(%[a-f0-9]{2,2}))*)' 452_path = '((\/([a-z0-9\-\.\_\~\!\$\&\'\(\)\*\+\,\;\=\:\@]|(%[a-f0-9]{2,2}))*)*)' 453_port = '(:([0-9]+))' 454_regname = '([a-z0-9\-\.\_\~]|\%[0-9a-f][0-9a-f]|[\!\$\&\'\(\)\*\+\,\;\=])' 455_ipv4 = '(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3,3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])' 456_ipvfuture = 'v[a-f0-9]+\.([a-z0-9\-\.\_\~]|[\!\$\&\'\(\)\*\+\,\;\=]|\:)+' 457_uri = '^' + _scheme +':(\/\/(' + _userinfo + '?(\[(' + _ipv6 + '|' + _ipvfuture + ')\]|' \ 458 + _ipv4 + '|' + _regname + '*)' + _port + '?)' + _path + ')' + _query + '?' + _fragment + '?$' 459"""
class
Validator:
56class Validator: 57 '''Validator class contains static methods. 58 Each method is associated to a global Datatype (name before the _) and 59 return a boolean (type conformity).''' 60 61 def json_valid(val): 62 return isinstance(val, (float, int, str, dict, list, bool)) or val is None 63 64 def number_valid(val): 65 return isinstance(val, (float, int)) 66 67 def boolean_valid(val): 68 return isinstance(val, bool) 69 70 def null_valid(val): 71 return val is None 72 73 def string_valid(val): 74 return isinstance(val, str) 75 76 def array_valid(val): 77 return isinstance(val, list) 78 79 def object_valid(val): 80 return isinstance(val, dict) 81 82 def int_valid(val): 83 return isinstance(val, int) 84 85 def int8_valid(val): 86 return isinstance(val, int) and -128 <= val <= 127 87 88 def int16_valid(val): 89 return isinstance(val, int) and -32768 <= val <= 32767 90 91 def int32_valid(val): 92 return isinstance(val, int) and -2147483648 <= val <= 2147483647 93 94 def int64_valid(val): 95 return isinstance(val, int) and -2 ^ 63 <= val <= 2 ^ 63 96 97 def uint8_valid(val): 98 return isinstance(val, int) and 0 <= val <= 255 99 100 def uint16_valid(val): 101 return isinstance(val, int) and 0 <= val <= 65535 102 103 def uint32_valid(val): 104 return isinstance(val, int) and 0 <= val <= 4294967295 105 106 def uint64_valid(val): 107 return isinstance(val, int) and 0 <= val <= 2 ^ 64 - 1 108 109 def float_valid(val): 110 return isinstance(val, float) 111 112 def float16_valid(val): 113 return isinstance(val, float) and abs(val) <= 65500 114 115 def float32_valid(val): 116 return isinstance(val, float) and abs(val) <= 3.4028237E38 117 118 def float64_valid(val): 119 return isinstance(val, float) 120 121 def decimal64_valid(val): 122 return isinstance(val, float) 123 124 def bit_valid(val): 125 if not isinstance(val, str): 126 return False 127 return val in ['0', '1'] 128 129 def binary_valid(val): 130 if not isinstance(val, str): 131 return False 132 for char in val: 133 if not char in ['0', '1']: 134 return False 135 return True 136 137 def base64_valid(val): 138 if not isinstance(val, str): 139 return False 140 for car in val: 141 if (not 'a' <= car <= 'z' and not 'A' <= car <= 'Z' and 142 not '0' <= car <= '9' and not car in ['-', '_', '=']): 143 return False 144 return True 145 146 def base32_valid(val): 147 if not isinstance(val, str): 148 return False 149 for car in val: 150 if not 'A' <= car <= 'Z' and not '1' < car < '8' and not car == '=': 151 return False 152 return True 153 154 def base16_valid(val): 155 if not isinstance(val, str): 156 return False 157 for car in val: 158 if not '0' <= car <= '9' and not 'A' <= car <= 'F': 159 return False 160 return True 161 162 def year_valid(val): 163 return isinstance(val, int) and 0 <= val 164 165 def month_valid(val): 166 return isinstance(val, int) and 0 < val < 13 167 168 def yearmonth_valid(val): 169 if not isinstance(val, str): 170 return False 171 y_m = val.split('-', maxsplit=1) 172 return Validator.year_valid(int(y_m[0])) and Validator.month_valid(int(y_m[1])) 173 174 def week_valid(val): 175 return isinstance(val, int) and 0 < val < 54 176 177 def day_valid(val): 178 return isinstance(val, int) and 0 < val < 32 179 180 def wday_valid(val): 181 return isinstance(val, int) and 0 < val < 8 182 183 def yday_valid(val): 184 return isinstance(val, int) and 0 < val < 367 185 186 def hour_valid(val): 187 return isinstance(val, int) and 0 <= val < 13 188 189 def minute_valid(val): 190 return isinstance(val, int) and 0 <= val < 60 191 192 def second_valid(val): 193 return isinstance(val, int) and 0 <= val < 60 194 195 def dat_valid(val): 196 return (Validator.date_valid(val) or Validator.time_valid(val) or 197 Validator.datetime_valid(val) or Validator.timetz_valid(val) or 198 Validator.datetimetz_valid(val)) 199 200 def date_valid(val): 201 try: 202 datetime.date.fromisoformat(val) 203 except ValueError: 204 return False 205 return True 206 207 def time_valid(val): 208 try: 209 tim = datetime.time.fromisoformat(val) 210 except ValueError: 211 return False 212 return True if not tim.tzinfo else False 213 214 def timetz_valid(val): 215 try: 216 tim = datetime.time.fromisoformat(val) 217 except ValueError: 218 return False 219 return True if tim.tzinfo else False 220 221 def datetime_valid(val): 222 try: 223 tim = datetime.datetime.fromisoformat(val) 224 except ValueError: 225 return False 226 return True if not tim.tzinfo else False 227 228 def datetimetz_valid(val): 229 try: 230 tim = datetime.datetime.fromisoformat(val) 231 except ValueError: 232 return False 233 return True if tim.tzinfo else False 234 235 def duration_valid(val): 236 if not isinstance(val, str): 237 return False 238 return DURATION.fullmatch(val) is not None 239 240 def period_valid(val): 241 if not isinstance(val, str): 242 return False 243 period = val.split('/', maxsplit=1) 244 for per in period: 245 if not (Validator.datetime_valid(per) or 246 Validator.datetimetz_valid(per) or 247 Validator.duration_valid(per)): 248 return False 249 if period[0][0] == 'P' and period[1][0] == 'P': 250 return False 251 return True 252 253 def timearray_valid(val): 254 return (isinstance(val, list) and len(val) == 2 and 255 Validator.dat_valid(val[0]) and Validator.dat_valid(val[1])) 256 257 def point_valid(val): 258 return (isinstance(val, list) and len(val) == 2 and 259 isinstance(val[0], (int, float)) and -180 <= val[0] <= 180 and 260 isinstance(val[1], (int, float)) and -180 <= val[1] <= 180) 261 262 def pointstr_valid(val): 263 if not isinstance(val, str): 264 return False 265 coord = val.split(',', maxsplit=1) 266 if len(coord) != 2: 267 return False 268 try: 269 point = [float(coord[0]), float(coord[1])] 270 except ValueError: 271 return False 272 return Validator.point_valid(point) 273 274 def pointobj_valid(val): 275 if not (isinstance(val, dict) and 'lon' in val and 'lat' in val): 276 return False 277 return Validator.point_valid([val['lon'], val['lat']]) 278 279 def multipoint_valid(val): 280 if not isinstance(val, list): 281 return False 282 for point in val: 283 if not Validator.point_valid(point): 284 return False 285 return True 286 287 def line_valid(val): 288 return Validator.multipoint_valid(val) 289 290 def multiline_valid(val): 291 if not isinstance(val, list): 292 return False 293 for line in val: 294 if not Validator.multipoint_valid(line): 295 return False 296 return True 297 298 def polygon_valid(val): 299 return Validator.multiline_valid(val) 300 301 def multipolygon_valid(val): 302 if not isinstance(val, list): 303 return False 304 for poly in val: 305 if not Validator.multiline_valid(poly): 306 return False 307 return True 308 309 def geometry_valid(val): 310 return (Validator.point_valid(val) or Validator.line_valid(val) or 311 Validator.polygon_valid(val)) 312 313 def multigeometry_valid(val): 314 if not isinstance(val, list): 315 return False 316 for geo in val: 317 if not Validator.geometry_valid(geo): 318 return False 319 return True 320 321 def box_valid(val): 322 if not (isinstance(val, list) and len(val) == 4): 323 return False 324 for coor in val: 325 if not (isinstance(coor, (int, float)) and -90 <= coor <= 90): 326 return False 327 return True 328 329 def geojson_valid(val): 330 if not (isinstance(val, dict) and 'type' in val): 331 return False 332 return val['type'] in GEOJSON and GEOJSON[val['type']] in val 333 334 def codeolc_valid(val): 335 if not isinstance(val, str): 336 return False 337 return OLC.fullmatch(val) is not None 338 339 def loc_valid(val): 340 return (Validator.point_valid(val) or Validator.pointstr_valid(val) or 341 Validator.pointobj_valid(val) or Validator.line_valid(val) or 342 Validator.polygon_valid(val) or Validator.multipolygon_valid(val) or 343 Validator.box_valid(val) or Validator.geojson_valid(val) or 344 Validator.codeolc_valid(val)) 345 346 def uri_valid(val): 347 if not isinstance(val, str): 348 return False 349 return URI.fullmatch(val) is not None 350 351 def uriref_valid(val): 352 if not isinstance(val, str): 353 return False 354 return URI.fullmatch(val) is not None 355 356 def iri_valid(val): 357 if not isinstance(val, str): 358 return False 359 return URI.fullmatch(val) is not None 360 361 def iriref_valid(val): 362 if not isinstance(val, str): 363 return False 364 return URI.fullmatch(val) is not None 365 366 def uritem_valid(val): 367 if not isinstance(val, str): 368 return False 369 return URI.fullmatch(val) is not None 370 371 def uuid_valid(val): 372 if not isinstance(val, str): 373 return False 374 return UUID.fullmatch(val) is not None 375 376 def email_valid(val): 377 if not isinstance(val, str): 378 return False 379 return ADDRESS.fullmatch(val) is not None 380 381 def hostname_valid(val): 382 if not isinstance(val, str) or len(val) > 253: 383 return False 384 return HOSTNAME.fullmatch(val) is not None 385 386 def jpointer_valid(val): 387 if not isinstance(val, str) or (len(val) > 0 and val[0] != '/'): 388 return False 389 return True 390 391 def ipv4_valid(val): 392 if not isinstance(val, str): 393 return False 394 return IPV4.fullmatch(val) is not None 395 396 def file_valid(val): 397 if not isinstance(val, str): 398 return False 399 return FILE.fullmatch(val) is not None 400 401 def ipv6_valid(val): 402 pass 403 404 def idnemail_valid(val): 405 pass 406 407 def idnhostname_valid(val): 408 pass 409 410 def rjpointer_valid(val): 411 pass 412 413 def regex_valid(val): 414 pass 415 416 def row_valid(val): 417 pass 418 419 def tab_valid(val): 420 pass 421 422 def field_valid(val): 423 pass 424 425 def ntv_valid(val): 426 pass 427 428 def sch_valid(val): 429 pass
Validator class contains static methods. Each method is associated to a global Datatype (name before the _) and return a boolean (type conformity).
def
period_valid(val):
240 def period_valid(val): 241 if not isinstance(val, str): 242 return False 243 period = val.split('/', maxsplit=1) 244 for per in period: 245 if not (Validator.datetime_valid(per) or 246 Validator.datetimetz_valid(per) or 247 Validator.duration_valid(per)): 248 return False 249 if period[0][0] == 'P' and period[1][0] == 'P': 250 return False 251 return True
def
loc_valid(val):
339 def loc_valid(val): 340 return (Validator.point_valid(val) or Validator.pointstr_valid(val) or 341 Validator.pointobj_valid(val) or Validator.line_valid(val) or 342 Validator.polygon_valid(val) or Validator.multipolygon_valid(val) or 343 Validator.box_valid(val) or Validator.geojson_valid(val) or 344 Validator.codeolc_valid(val))
class
ValidateError(builtins.Exception):
Validator exception
Inherited Members
- builtins.Exception
- Exception
- builtins.BaseException
- with_traceback