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 r'([2-90CFGHJMPQRVWX]{2}){4}\+([2-9CFGHJMPQRVWX]{2}[2-9CFGHJMPQRVWX]*)?') 37URI = re.compile(r'^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?') 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_DOC_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 = '((' + _DOC_ATOM + '|' + _QUOTED_STRING + \ 44 ')@(' + _DOC_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 r'(([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 @staticmethod 61 def json_valid(val): 62 '''validate method''' 63 return isinstance(val, (float, int, str, dict, list, bool)) or val is None 64 65 @staticmethod 66 def number_valid(val): 67 '''validate method''' 68 return isinstance(val, (float, int)) 69 70 @staticmethod 71 def boolean_valid(val): 72 '''validate method''' 73 return isinstance(val, bool) 74 75 @staticmethod 76 def null_valid(val): 77 '''validate method''' 78 return val is None 79 80 @staticmethod 81 def string_valid(val): 82 '''validate method''' 83 return isinstance(val, str) 84 85 @staticmethod 86 def array_valid(val): 87 '''validate method''' 88 return isinstance(val, list) 89 90 @staticmethod 91 def object_valid(val): 92 '''validate method''' 93 return isinstance(val, dict) 94 95 @staticmethod 96 def int_valid(val): 97 '''validate method''' 98 return isinstance(val, int) 99 100 @staticmethod 101 def int8_valid(val): 102 '''validate method''' 103 return isinstance(val, int) and -128 <= val <= 127 104 105 @staticmethod 106 def int16_valid(val): 107 '''validate method''' 108 return isinstance(val, int) and -32768 <= val <= 32767 109 110 @staticmethod 111 def int32_valid(val): 112 '''validate method''' 113 return isinstance(val, int) and -2147483648 <= val <= 2147483647 114 115 @staticmethod 116 def int64_valid(val): 117 '''validate method''' 118 return isinstance(val, int) and -2 ^ 63 <= val <= 2 ^ 63 119 120 @staticmethod 121 def uint8_valid(val): 122 '''validate method''' 123 return isinstance(val, int) and 0 <= val <= 255 124 125 @staticmethod 126 def uint16_valid(val): 127 '''validate method''' 128 return isinstance(val, int) and 0 <= val <= 65535 129 130 @staticmethod 131 def uint32_valid(val): 132 '''validate method''' 133 return isinstance(val, int) and 0 <= val <= 4294967295 134 135 @staticmethod 136 def uint64_valid(val): 137 '''validate method''' 138 return isinstance(val, int) and 0 <= val <= 2 ^ 64 - 1 139 140 @staticmethod 141 def float_valid(val): 142 '''validate method''' 143 return isinstance(val, float) 144 145 @staticmethod 146 def float16_valid(val): 147 '''validate method''' 148 return isinstance(val, float) and abs(val) <= 65500 149 150 @staticmethod 151 def float32_valid(val): 152 '''validate method''' 153 return isinstance(val, float) and abs(val) <= 3.4028237E38 154 155 @staticmethod 156 def float64_valid(val): 157 '''validate method''' 158 return isinstance(val, float) 159 160 @staticmethod 161 def decimal64_valid(val): 162 '''validate method''' 163 return isinstance(val, float) 164 165 @staticmethod 166 def bit_valid(val): 167 '''validate method''' 168 if not isinstance(val, str): 169 return False 170 return val in ['0', '1'] 171 172 @staticmethod 173 def binary_valid(val): 174 '''validate method''' 175 if not isinstance(val, str): 176 return False 177 for char in val: 178 if char not in ['0', '1']: 179 return False 180 return True 181 182 @staticmethod 183 def base64_valid(val): 184 '''validate method''' 185 if not isinstance(val, str): 186 return False 187 for car in val: 188 if (not 'a' <= car <= 'z' and not 'A' <= car <= 'Z' and 189 not '0' <= car <= '9' and car not in ['-', '_', '=']): 190 return False 191 return True 192 193 @staticmethod 194 def base32_valid(val): 195 '''validate method''' 196 if not isinstance(val, str): 197 return False 198 for car in val: 199 if not 'A' <= car <= 'Z' and not '1' < car < '8' and not car == '=': 200 return False 201 return True 202 203 @staticmethod 204 def base16_valid(val): 205 '''validate method''' 206 if not isinstance(val, str): 207 return False 208 for car in val: 209 if not '0' <= car <= '9' and not 'A' <= car <= 'F': 210 return False 211 return True 212 213 @staticmethod 214 def year_valid(val): 215 '''validate method''' 216 return isinstance(val, int) and 0 <= val 217 218 @staticmethod 219 def month_valid(val): 220 '''validate method''' 221 return isinstance(val, int) and 0 < val < 13 222 223 @staticmethod 224 def yearmonth_valid(val): 225 '''validate method''' 226 if not isinstance(val, str): 227 return False 228 y_m = val.split('-', maxsplit=1) 229 return Validator.year_valid(int(y_m[0])) and Validator.month_valid(int(y_m[1])) 230 231 @staticmethod 232 def week_valid(val): 233 '''validate method''' 234 return isinstance(val, int) and 0 < val < 54 235 236 @staticmethod 237 def day_valid(val): 238 '''validate method''' 239 return isinstance(val, int) and 0 < val < 32 240 241 @staticmethod 242 def wday_valid(val): 243 '''validate method''' 244 return isinstance(val, int) and 0 < val < 8 245 246 @staticmethod 247 def yday_valid(val): 248 '''validate method''' 249 return isinstance(val, int) and 0 < val < 367 250 251 @staticmethod 252 def hour_valid(val): 253 '''validate method''' 254 return isinstance(val, int) and 0 <= val < 13 255 256 @staticmethod 257 def minute_valid(val): 258 '''validate method''' 259 return isinstance(val, int) and 0 <= val < 60 260 261 @staticmethod 262 def second_valid(val): 263 '''validate method''' 264 return isinstance(val, int) and 0 <= val < 60 265 266 @staticmethod 267 def dat_valid(val): 268 '''validate method''' 269 return (Validator.date_valid(val) or Validator.time_valid(val) or 270 Validator.datetime_valid(val) or Validator.timetz_valid(val) or 271 Validator.datetimetz_valid(val)) 272 273 @staticmethod 274 def date_valid(val): 275 '''validate method''' 276 try: 277 datetime.date.fromisoformat(val) 278 except ValueError: 279 return False 280 return True 281 282 @staticmethod 283 def time_valid(val): 284 '''validate method''' 285 try: 286 tim = datetime.time.fromisoformat(val) 287 except ValueError: 288 return False 289 # return True if not tim.tzinfo else False 290 return bool(not tim.tzinfo) 291 292 @staticmethod 293 def timetz_valid(val): 294 '''validate method''' 295 try: 296 tim = datetime.time.fromisoformat(val) 297 except ValueError: 298 return False 299 # return True if tim.tzinfo else False 300 return bool(tim.tzinfo) 301 302 @staticmethod 303 def datetime_valid(val): 304 '''validate method''' 305 try: 306 tim = datetime.datetime.fromisoformat(val) 307 except ValueError: 308 return False 309 # return True if not tim.tzinfo else False 310 return bool(not tim.tzinfo) 311 312 @staticmethod 313 def datetimetz_valid(val): 314 '''validate method''' 315 try: 316 tim = datetime.datetime.fromisoformat(val) 317 except ValueError: 318 return False 319 # return True if tim.tzinfo else False 320 return bool(tim.tzinfo) 321 322 @staticmethod 323 def duration_valid(val): 324 '''validate method''' 325 if not isinstance(val, str): 326 return False 327 return DURATION.fullmatch(val) is not None 328 329 @staticmethod 330 def period_valid(val): 331 '''validate method''' 332 if not isinstance(val, str): 333 return False 334 period = val.split('/', maxsplit=1) 335 for per in period: 336 if not (Validator.datetime_valid(per) or 337 Validator.datetimetz_valid(per) or 338 Validator.duration_valid(per)): 339 return False 340 if period[0][0] == 'P' and period[1][0] == 'P': 341 return False 342 return True 343 344 @staticmethod 345 def timearray_valid(val): 346 '''validate method''' 347 return (isinstance(val, list) and len(val) == 2 and 348 Validator.dat_valid(val[0]) and Validator.dat_valid(val[1])) 349 350 @staticmethod 351 def point_valid(val): 352 '''validate method''' 353 return (isinstance(val, list) and len(val) == 2 and 354 isinstance(val[0], (int, float)) and -180 <= val[0] <= 180 and 355 isinstance(val[1], (int, float)) and -180 <= val[1] <= 180) 356 357 @staticmethod 358 def pointstr_valid(val): 359 '''validate method''' 360 if not isinstance(val, str): 361 return False 362 coord = val.split(',', maxsplit=1) 363 if len(coord) != 2: 364 return False 365 try: 366 point = [float(coord[0]), float(coord[1])] 367 except ValueError: 368 return False 369 return Validator.point_valid(point) 370 371 @staticmethod 372 def pointobj_valid(val): 373 '''validate method''' 374 if not (isinstance(val, dict) and 'lon' in val and 'lat' in val): 375 return False 376 return Validator.point_valid([val['lon'], val['lat']]) 377 378 @staticmethod 379 def multipoint_valid(val): 380 '''validate method''' 381 if not isinstance(val, list): 382 return False 383 for point in val: 384 if not Validator.point_valid(point): 385 return False 386 return True 387 388 @staticmethod 389 def line_valid(val): 390 '''validate method''' 391 return Validator.multipoint_valid(val) 392 393 @staticmethod 394 def multiline_valid(val): 395 '''validate method''' 396 if not isinstance(val, list): 397 return False 398 for line in val: 399 if not Validator.multipoint_valid(line): 400 return False 401 return True 402 403 @staticmethod 404 def polygon_valid(val): 405 '''validate method''' 406 return Validator.multiline_valid(val) 407 408 @staticmethod 409 def multipolygon_valid(val): 410 '''validate method''' 411 if not isinstance(val, list): 412 return False 413 for poly in val: 414 if not Validator.multiline_valid(poly): 415 return False 416 return True 417 418 @staticmethod 419 def geometry_valid(val): 420 '''validate method''' 421 return (Validator.point_valid(val) or Validator.line_valid(val) or 422 Validator.polygon_valid(val)) 423 424 @staticmethod 425 def multigeometry_valid(val): 426 '''validate method''' 427 if not isinstance(val, list): 428 return False 429 for geo in val: 430 if not Validator.geometry_valid(geo): 431 return False 432 return True 433 434 @staticmethod 435 def box_valid(val): 436 '''validate method''' 437 if not (isinstance(val, list) and len(val) == 4): 438 return False 439 for coor in val: 440 if not (isinstance(coor, (int, float)) and -90 <= coor <= 90): 441 return False 442 return True 443 444 @staticmethod 445 def geojson_valid(val): 446 '''validate method''' 447 if not (isinstance(val, dict) and 'type' in val): 448 return False 449 return val['type'] in GEOJSON and GEOJSON[val['type']] in val 450 451 @staticmethod 452 def codeolc_valid(val): 453 '''validate method''' 454 if not isinstance(val, str): 455 return False 456 return OLC.fullmatch(val) is not None 457 458 @staticmethod 459 def loc_valid(val): 460 '''validate method''' 461 return (Validator.point_valid(val) or Validator.pointstr_valid(val) or 462 Validator.pointobj_valid(val) or Validator.line_valid(val) or 463 Validator.polygon_valid(val) or Validator.multipolygon_valid(val) or 464 Validator.box_valid(val) or Validator.geojson_valid(val) or 465 Validator.codeolc_valid(val)) 466 467 @staticmethod 468 def unit_valid(val): 469 '''validate method''' 470 return isinstance(val, str) 471 472 @staticmethod 473 def uri_valid(val): 474 '''validate method''' 475 if not isinstance(val, str): 476 return False 477 return URI.fullmatch(val) is not None 478 479 @staticmethod 480 def uriref_valid(val): 481 '''validate method''' 482 if not isinstance(val, str): 483 return False 484 return URI.fullmatch(val) is not None 485 486 @staticmethod 487 def iri_valid(val): 488 '''validate method''' 489 if not isinstance(val, str): 490 return False 491 return URI.fullmatch(val) is not None 492 493 @staticmethod 494 def iriref_valid(val): 495 '''validate method''' 496 if not isinstance(val, str): 497 return False 498 return URI.fullmatch(val) is not None 499 500 @staticmethod 501 def uritem_valid(val): 502 '''validate method''' 503 if not isinstance(val, str): 504 return False 505 return URI.fullmatch(val) is not None 506 507 @staticmethod 508 def uuid_valid(val): 509 '''validate method''' 510 if not isinstance(val, str): 511 return False 512 return UUID.fullmatch(val) is not None 513 514 @staticmethod 515 def email_valid(val): 516 '''validate method''' 517 if not isinstance(val, str): 518 return False 519 return ADDRESS.fullmatch(val) is not None 520 521 @staticmethod 522 def hostname_valid(val): 523 '''validate method''' 524 if not isinstance(val, str) or len(val) > 253: 525 return False 526 return HOSTNAME.fullmatch(val) is not None 527 528 @staticmethod 529 def jpointer_valid(val): 530 '''validate method''' 531 if not isinstance(val, str) or (len(val) > 0 and val[0] != '/'): 532 return False 533 return True 534 535 @staticmethod 536 def ipv4_valid(val): 537 '''validate method''' 538 if not isinstance(val, str): 539 return False 540 return IPV4.fullmatch(val) is not None 541 542 @staticmethod 543 def file_valid(val): 544 '''validate method''' 545 if not isinstance(val, str): 546 return False 547 return FILE.fullmatch(val) is not None 548 549 @staticmethod 550 def ipv6_valid(val): 551 '''validate method''' 552 553 @staticmethod 554 def idnemail_valid(val): 555 '''validate method''' 556 557 @staticmethod 558 def idnhostname_valid(val): 559 '''validate method''' 560 561 @staticmethod 562 def rjpointer_valid(val): 563 '''validate method''' 564 565 @staticmethod 566 def regex_valid(val): 567 '''validate method''' 568 569 @staticmethod 570 def row_valid(val): 571 '''validate method''' 572 573 @staticmethod 574 def tab_valid(val): 575 '''validate method''' 576 577 @staticmethod 578 def field_valid(val): 579 '''validate method''' 580 581 @staticmethod 582 def ntv_valid(val): 583 '''validate method''' 584 585 @staticmethod 586 def sch_valid(val): 587 '''validate method''' 588 589 @staticmethod 590 def narray_valid(val): 591 '''validate method''' 592 593 @staticmethod 594 def ndarray_valid(val): 595 '''validate method''' 596 597 @staticmethod 598 def xndarray_valid(val): 599 '''validate method''' 600 601 @staticmethod 602 def xdataset_valid(val): 603 '''validate method''' 604 605 606class ValidateError(Exception): 607 '''Validator exception'''
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 @staticmethod 62 def json_valid(val): 63 '''validate method''' 64 return isinstance(val, (float, int, str, dict, list, bool)) or val is None 65 66 @staticmethod 67 def number_valid(val): 68 '''validate method''' 69 return isinstance(val, (float, int)) 70 71 @staticmethod 72 def boolean_valid(val): 73 '''validate method''' 74 return isinstance(val, bool) 75 76 @staticmethod 77 def null_valid(val): 78 '''validate method''' 79 return val is None 80 81 @staticmethod 82 def string_valid(val): 83 '''validate method''' 84 return isinstance(val, str) 85 86 @staticmethod 87 def array_valid(val): 88 '''validate method''' 89 return isinstance(val, list) 90 91 @staticmethod 92 def object_valid(val): 93 '''validate method''' 94 return isinstance(val, dict) 95 96 @staticmethod 97 def int_valid(val): 98 '''validate method''' 99 return isinstance(val, int) 100 101 @staticmethod 102 def int8_valid(val): 103 '''validate method''' 104 return isinstance(val, int) and -128 <= val <= 127 105 106 @staticmethod 107 def int16_valid(val): 108 '''validate method''' 109 return isinstance(val, int) and -32768 <= val <= 32767 110 111 @staticmethod 112 def int32_valid(val): 113 '''validate method''' 114 return isinstance(val, int) and -2147483648 <= val <= 2147483647 115 116 @staticmethod 117 def int64_valid(val): 118 '''validate method''' 119 return isinstance(val, int) and -2 ^ 63 <= val <= 2 ^ 63 120 121 @staticmethod 122 def uint8_valid(val): 123 '''validate method''' 124 return isinstance(val, int) and 0 <= val <= 255 125 126 @staticmethod 127 def uint16_valid(val): 128 '''validate method''' 129 return isinstance(val, int) and 0 <= val <= 65535 130 131 @staticmethod 132 def uint32_valid(val): 133 '''validate method''' 134 return isinstance(val, int) and 0 <= val <= 4294967295 135 136 @staticmethod 137 def uint64_valid(val): 138 '''validate method''' 139 return isinstance(val, int) and 0 <= val <= 2 ^ 64 - 1 140 141 @staticmethod 142 def float_valid(val): 143 '''validate method''' 144 return isinstance(val, float) 145 146 @staticmethod 147 def float16_valid(val): 148 '''validate method''' 149 return isinstance(val, float) and abs(val) <= 65500 150 151 @staticmethod 152 def float32_valid(val): 153 '''validate method''' 154 return isinstance(val, float) and abs(val) <= 3.4028237E38 155 156 @staticmethod 157 def float64_valid(val): 158 '''validate method''' 159 return isinstance(val, float) 160 161 @staticmethod 162 def decimal64_valid(val): 163 '''validate method''' 164 return isinstance(val, float) 165 166 @staticmethod 167 def bit_valid(val): 168 '''validate method''' 169 if not isinstance(val, str): 170 return False 171 return val in ['0', '1'] 172 173 @staticmethod 174 def binary_valid(val): 175 '''validate method''' 176 if not isinstance(val, str): 177 return False 178 for char in val: 179 if char not in ['0', '1']: 180 return False 181 return True 182 183 @staticmethod 184 def base64_valid(val): 185 '''validate method''' 186 if not isinstance(val, str): 187 return False 188 for car in val: 189 if (not 'a' <= car <= 'z' and not 'A' <= car <= 'Z' and 190 not '0' <= car <= '9' and car not in ['-', '_', '=']): 191 return False 192 return True 193 194 @staticmethod 195 def base32_valid(val): 196 '''validate method''' 197 if not isinstance(val, str): 198 return False 199 for car in val: 200 if not 'A' <= car <= 'Z' and not '1' < car < '8' and not car == '=': 201 return False 202 return True 203 204 @staticmethod 205 def base16_valid(val): 206 '''validate method''' 207 if not isinstance(val, str): 208 return False 209 for car in val: 210 if not '0' <= car <= '9' and not 'A' <= car <= 'F': 211 return False 212 return True 213 214 @staticmethod 215 def year_valid(val): 216 '''validate method''' 217 return isinstance(val, int) and 0 <= val 218 219 @staticmethod 220 def month_valid(val): 221 '''validate method''' 222 return isinstance(val, int) and 0 < val < 13 223 224 @staticmethod 225 def yearmonth_valid(val): 226 '''validate method''' 227 if not isinstance(val, str): 228 return False 229 y_m = val.split('-', maxsplit=1) 230 return Validator.year_valid(int(y_m[0])) and Validator.month_valid(int(y_m[1])) 231 232 @staticmethod 233 def week_valid(val): 234 '''validate method''' 235 return isinstance(val, int) and 0 < val < 54 236 237 @staticmethod 238 def day_valid(val): 239 '''validate method''' 240 return isinstance(val, int) and 0 < val < 32 241 242 @staticmethod 243 def wday_valid(val): 244 '''validate method''' 245 return isinstance(val, int) and 0 < val < 8 246 247 @staticmethod 248 def yday_valid(val): 249 '''validate method''' 250 return isinstance(val, int) and 0 < val < 367 251 252 @staticmethod 253 def hour_valid(val): 254 '''validate method''' 255 return isinstance(val, int) and 0 <= val < 13 256 257 @staticmethod 258 def minute_valid(val): 259 '''validate method''' 260 return isinstance(val, int) and 0 <= val < 60 261 262 @staticmethod 263 def second_valid(val): 264 '''validate method''' 265 return isinstance(val, int) and 0 <= val < 60 266 267 @staticmethod 268 def dat_valid(val): 269 '''validate method''' 270 return (Validator.date_valid(val) or Validator.time_valid(val) or 271 Validator.datetime_valid(val) or Validator.timetz_valid(val) or 272 Validator.datetimetz_valid(val)) 273 274 @staticmethod 275 def date_valid(val): 276 '''validate method''' 277 try: 278 datetime.date.fromisoformat(val) 279 except ValueError: 280 return False 281 return True 282 283 @staticmethod 284 def time_valid(val): 285 '''validate method''' 286 try: 287 tim = datetime.time.fromisoformat(val) 288 except ValueError: 289 return False 290 # return True if not tim.tzinfo else False 291 return bool(not tim.tzinfo) 292 293 @staticmethod 294 def timetz_valid(val): 295 '''validate method''' 296 try: 297 tim = datetime.time.fromisoformat(val) 298 except ValueError: 299 return False 300 # return True if tim.tzinfo else False 301 return bool(tim.tzinfo) 302 303 @staticmethod 304 def datetime_valid(val): 305 '''validate method''' 306 try: 307 tim = datetime.datetime.fromisoformat(val) 308 except ValueError: 309 return False 310 # return True if not tim.tzinfo else False 311 return bool(not tim.tzinfo) 312 313 @staticmethod 314 def datetimetz_valid(val): 315 '''validate method''' 316 try: 317 tim = datetime.datetime.fromisoformat(val) 318 except ValueError: 319 return False 320 # return True if tim.tzinfo else False 321 return bool(tim.tzinfo) 322 323 @staticmethod 324 def duration_valid(val): 325 '''validate method''' 326 if not isinstance(val, str): 327 return False 328 return DURATION.fullmatch(val) is not None 329 330 @staticmethod 331 def period_valid(val): 332 '''validate method''' 333 if not isinstance(val, str): 334 return False 335 period = val.split('/', maxsplit=1) 336 for per in period: 337 if not (Validator.datetime_valid(per) or 338 Validator.datetimetz_valid(per) or 339 Validator.duration_valid(per)): 340 return False 341 if period[0][0] == 'P' and period[1][0] == 'P': 342 return False 343 return True 344 345 @staticmethod 346 def timearray_valid(val): 347 '''validate method''' 348 return (isinstance(val, list) and len(val) == 2 and 349 Validator.dat_valid(val[0]) and Validator.dat_valid(val[1])) 350 351 @staticmethod 352 def point_valid(val): 353 '''validate method''' 354 return (isinstance(val, list) and len(val) == 2 and 355 isinstance(val[0], (int, float)) and -180 <= val[0] <= 180 and 356 isinstance(val[1], (int, float)) and -180 <= val[1] <= 180) 357 358 @staticmethod 359 def pointstr_valid(val): 360 '''validate method''' 361 if not isinstance(val, str): 362 return False 363 coord = val.split(',', maxsplit=1) 364 if len(coord) != 2: 365 return False 366 try: 367 point = [float(coord[0]), float(coord[1])] 368 except ValueError: 369 return False 370 return Validator.point_valid(point) 371 372 @staticmethod 373 def pointobj_valid(val): 374 '''validate method''' 375 if not (isinstance(val, dict) and 'lon' in val and 'lat' in val): 376 return False 377 return Validator.point_valid([val['lon'], val['lat']]) 378 379 @staticmethod 380 def multipoint_valid(val): 381 '''validate method''' 382 if not isinstance(val, list): 383 return False 384 for point in val: 385 if not Validator.point_valid(point): 386 return False 387 return True 388 389 @staticmethod 390 def line_valid(val): 391 '''validate method''' 392 return Validator.multipoint_valid(val) 393 394 @staticmethod 395 def multiline_valid(val): 396 '''validate method''' 397 if not isinstance(val, list): 398 return False 399 for line in val: 400 if not Validator.multipoint_valid(line): 401 return False 402 return True 403 404 @staticmethod 405 def polygon_valid(val): 406 '''validate method''' 407 return Validator.multiline_valid(val) 408 409 @staticmethod 410 def multipolygon_valid(val): 411 '''validate method''' 412 if not isinstance(val, list): 413 return False 414 for poly in val: 415 if not Validator.multiline_valid(poly): 416 return False 417 return True 418 419 @staticmethod 420 def geometry_valid(val): 421 '''validate method''' 422 return (Validator.point_valid(val) or Validator.line_valid(val) or 423 Validator.polygon_valid(val)) 424 425 @staticmethod 426 def multigeometry_valid(val): 427 '''validate method''' 428 if not isinstance(val, list): 429 return False 430 for geo in val: 431 if not Validator.geometry_valid(geo): 432 return False 433 return True 434 435 @staticmethod 436 def box_valid(val): 437 '''validate method''' 438 if not (isinstance(val, list) and len(val) == 4): 439 return False 440 for coor in val: 441 if not (isinstance(coor, (int, float)) and -90 <= coor <= 90): 442 return False 443 return True 444 445 @staticmethod 446 def geojson_valid(val): 447 '''validate method''' 448 if not (isinstance(val, dict) and 'type' in val): 449 return False 450 return val['type'] in GEOJSON and GEOJSON[val['type']] in val 451 452 @staticmethod 453 def codeolc_valid(val): 454 '''validate method''' 455 if not isinstance(val, str): 456 return False 457 return OLC.fullmatch(val) is not None 458 459 @staticmethod 460 def loc_valid(val): 461 '''validate method''' 462 return (Validator.point_valid(val) or Validator.pointstr_valid(val) or 463 Validator.pointobj_valid(val) or Validator.line_valid(val) or 464 Validator.polygon_valid(val) or Validator.multipolygon_valid(val) or 465 Validator.box_valid(val) or Validator.geojson_valid(val) or 466 Validator.codeolc_valid(val)) 467 468 @staticmethod 469 def unit_valid(val): 470 '''validate method''' 471 return isinstance(val, str) 472 473 @staticmethod 474 def uri_valid(val): 475 '''validate method''' 476 if not isinstance(val, str): 477 return False 478 return URI.fullmatch(val) is not None 479 480 @staticmethod 481 def uriref_valid(val): 482 '''validate method''' 483 if not isinstance(val, str): 484 return False 485 return URI.fullmatch(val) is not None 486 487 @staticmethod 488 def iri_valid(val): 489 '''validate method''' 490 if not isinstance(val, str): 491 return False 492 return URI.fullmatch(val) is not None 493 494 @staticmethod 495 def iriref_valid(val): 496 '''validate method''' 497 if not isinstance(val, str): 498 return False 499 return URI.fullmatch(val) is not None 500 501 @staticmethod 502 def uritem_valid(val): 503 '''validate method''' 504 if not isinstance(val, str): 505 return False 506 return URI.fullmatch(val) is not None 507 508 @staticmethod 509 def uuid_valid(val): 510 '''validate method''' 511 if not isinstance(val, str): 512 return False 513 return UUID.fullmatch(val) is not None 514 515 @staticmethod 516 def email_valid(val): 517 '''validate method''' 518 if not isinstance(val, str): 519 return False 520 return ADDRESS.fullmatch(val) is not None 521 522 @staticmethod 523 def hostname_valid(val): 524 '''validate method''' 525 if not isinstance(val, str) or len(val) > 253: 526 return False 527 return HOSTNAME.fullmatch(val) is not None 528 529 @staticmethod 530 def jpointer_valid(val): 531 '''validate method''' 532 if not isinstance(val, str) or (len(val) > 0 and val[0] != '/'): 533 return False 534 return True 535 536 @staticmethod 537 def ipv4_valid(val): 538 '''validate method''' 539 if not isinstance(val, str): 540 return False 541 return IPV4.fullmatch(val) is not None 542 543 @staticmethod 544 def file_valid(val): 545 '''validate method''' 546 if not isinstance(val, str): 547 return False 548 return FILE.fullmatch(val) is not None 549 550 @staticmethod 551 def ipv6_valid(val): 552 '''validate method''' 553 554 @staticmethod 555 def idnemail_valid(val): 556 '''validate method''' 557 558 @staticmethod 559 def idnhostname_valid(val): 560 '''validate method''' 561 562 @staticmethod 563 def rjpointer_valid(val): 564 '''validate method''' 565 566 @staticmethod 567 def regex_valid(val): 568 '''validate method''' 569 570 @staticmethod 571 def row_valid(val): 572 '''validate method''' 573 574 @staticmethod 575 def tab_valid(val): 576 '''validate method''' 577 578 @staticmethod 579 def field_valid(val): 580 '''validate method''' 581 582 @staticmethod 583 def ntv_valid(val): 584 '''validate method''' 585 586 @staticmethod 587 def sch_valid(val): 588 '''validate method''' 589 590 @staticmethod 591 def narray_valid(val): 592 '''validate method''' 593 594 @staticmethod 595 def ndarray_valid(val): 596 '''validate method''' 597 598 @staticmethod 599 def xndarray_valid(val): 600 '''validate method''' 601 602 @staticmethod 603 def xdataset_valid(val): 604 '''validate method'''
Validator class contains static methods. Each method is associated to a global Datatype (name before the _) and return a boolean (type conformity).
61 @staticmethod 62 def json_valid(val): 63 '''validate method''' 64 return isinstance(val, (float, int, str, dict, list, bool)) or val is None
validate method
66 @staticmethod 67 def number_valid(val): 68 '''validate method''' 69 return isinstance(val, (float, int))
validate method
71 @staticmethod 72 def boolean_valid(val): 73 '''validate method''' 74 return isinstance(val, bool)
validate method
101 @staticmethod 102 def int8_valid(val): 103 '''validate method''' 104 return isinstance(val, int) and -128 <= val <= 127
validate method
106 @staticmethod 107 def int16_valid(val): 108 '''validate method''' 109 return isinstance(val, int) and -32768 <= val <= 32767
validate method
111 @staticmethod 112 def int32_valid(val): 113 '''validate method''' 114 return isinstance(val, int) and -2147483648 <= val <= 2147483647
validate method
116 @staticmethod 117 def int64_valid(val): 118 '''validate method''' 119 return isinstance(val, int) and -2 ^ 63 <= val <= 2 ^ 63
validate method
121 @staticmethod 122 def uint8_valid(val): 123 '''validate method''' 124 return isinstance(val, int) and 0 <= val <= 255
validate method
126 @staticmethod 127 def uint16_valid(val): 128 '''validate method''' 129 return isinstance(val, int) and 0 <= val <= 65535
validate method
131 @staticmethod 132 def uint32_valid(val): 133 '''validate method''' 134 return isinstance(val, int) and 0 <= val <= 4294967295
validate method
136 @staticmethod 137 def uint64_valid(val): 138 '''validate method''' 139 return isinstance(val, int) and 0 <= val <= 2 ^ 64 - 1
validate method
141 @staticmethod 142 def float_valid(val): 143 '''validate method''' 144 return isinstance(val, float)
validate method
146 @staticmethod 147 def float16_valid(val): 148 '''validate method''' 149 return isinstance(val, float) and abs(val) <= 65500
validate method
151 @staticmethod 152 def float32_valid(val): 153 '''validate method''' 154 return isinstance(val, float) and abs(val) <= 3.4028237E38
validate method
156 @staticmethod 157 def float64_valid(val): 158 '''validate method''' 159 return isinstance(val, float)
validate method
161 @staticmethod 162 def decimal64_valid(val): 163 '''validate method''' 164 return isinstance(val, float)
validate method
166 @staticmethod 167 def bit_valid(val): 168 '''validate method''' 169 if not isinstance(val, str): 170 return False 171 return val in ['0', '1']
validate method
173 @staticmethod 174 def binary_valid(val): 175 '''validate method''' 176 if not isinstance(val, str): 177 return False 178 for char in val: 179 if char not in ['0', '1']: 180 return False 181 return True
validate method
183 @staticmethod 184 def base64_valid(val): 185 '''validate method''' 186 if not isinstance(val, str): 187 return False 188 for car in val: 189 if (not 'a' <= car <= 'z' and not 'A' <= car <= 'Z' and 190 not '0' <= car <= '9' and car not in ['-', '_', '=']): 191 return False 192 return True
validate method
194 @staticmethod 195 def base32_valid(val): 196 '''validate method''' 197 if not isinstance(val, str): 198 return False 199 for car in val: 200 if not 'A' <= car <= 'Z' and not '1' < car < '8' and not car == '=': 201 return False 202 return True
validate method
204 @staticmethod 205 def base16_valid(val): 206 '''validate method''' 207 if not isinstance(val, str): 208 return False 209 for car in val: 210 if not '0' <= car <= '9' and not 'A' <= car <= 'F': 211 return False 212 return True
validate method
214 @staticmethod 215 def year_valid(val): 216 '''validate method''' 217 return isinstance(val, int) and 0 <= val
validate method
219 @staticmethod 220 def month_valid(val): 221 '''validate method''' 222 return isinstance(val, int) and 0 < val < 13
validate method
224 @staticmethod 225 def yearmonth_valid(val): 226 '''validate method''' 227 if not isinstance(val, str): 228 return False 229 y_m = val.split('-', maxsplit=1) 230 return Validator.year_valid(int(y_m[0])) and Validator.month_valid(int(y_m[1]))
validate method
232 @staticmethod 233 def week_valid(val): 234 '''validate method''' 235 return isinstance(val, int) and 0 < val < 54
validate method
237 @staticmethod 238 def day_valid(val): 239 '''validate method''' 240 return isinstance(val, int) and 0 < val < 32
validate method
242 @staticmethod 243 def wday_valid(val): 244 '''validate method''' 245 return isinstance(val, int) and 0 < val < 8
validate method
247 @staticmethod 248 def yday_valid(val): 249 '''validate method''' 250 return isinstance(val, int) and 0 < val < 367
validate method
252 @staticmethod 253 def hour_valid(val): 254 '''validate method''' 255 return isinstance(val, int) and 0 <= val < 13
validate method
257 @staticmethod 258 def minute_valid(val): 259 '''validate method''' 260 return isinstance(val, int) and 0 <= val < 60
validate method
262 @staticmethod 263 def second_valid(val): 264 '''validate method''' 265 return isinstance(val, int) and 0 <= val < 60
validate method
267 @staticmethod 268 def dat_valid(val): 269 '''validate method''' 270 return (Validator.date_valid(val) or Validator.time_valid(val) or 271 Validator.datetime_valid(val) or Validator.timetz_valid(val) or 272 Validator.datetimetz_valid(val))
validate method
274 @staticmethod 275 def date_valid(val): 276 '''validate method''' 277 try: 278 datetime.date.fromisoformat(val) 279 except ValueError: 280 return False 281 return True
validate method
283 @staticmethod 284 def time_valid(val): 285 '''validate method''' 286 try: 287 tim = datetime.time.fromisoformat(val) 288 except ValueError: 289 return False 290 # return True if not tim.tzinfo else False 291 return bool(not tim.tzinfo)
validate method
293 @staticmethod 294 def timetz_valid(val): 295 '''validate method''' 296 try: 297 tim = datetime.time.fromisoformat(val) 298 except ValueError: 299 return False 300 # return True if tim.tzinfo else False 301 return bool(tim.tzinfo)
validate method
303 @staticmethod 304 def datetime_valid(val): 305 '''validate method''' 306 try: 307 tim = datetime.datetime.fromisoformat(val) 308 except ValueError: 309 return False 310 # return True if not tim.tzinfo else False 311 return bool(not tim.tzinfo)
validate method
313 @staticmethod 314 def datetimetz_valid(val): 315 '''validate method''' 316 try: 317 tim = datetime.datetime.fromisoformat(val) 318 except ValueError: 319 return False 320 # return True if tim.tzinfo else False 321 return bool(tim.tzinfo)
validate method
323 @staticmethod 324 def duration_valid(val): 325 '''validate method''' 326 if not isinstance(val, str): 327 return False 328 return DURATION.fullmatch(val) is not None
validate method
330 @staticmethod 331 def period_valid(val): 332 '''validate method''' 333 if not isinstance(val, str): 334 return False 335 period = val.split('/', maxsplit=1) 336 for per in period: 337 if not (Validator.datetime_valid(per) or 338 Validator.datetimetz_valid(per) or 339 Validator.duration_valid(per)): 340 return False 341 if period[0][0] == 'P' and period[1][0] == 'P': 342 return False 343 return True
validate method
345 @staticmethod 346 def timearray_valid(val): 347 '''validate method''' 348 return (isinstance(val, list) and len(val) == 2 and 349 Validator.dat_valid(val[0]) and Validator.dat_valid(val[1]))
validate method
351 @staticmethod 352 def point_valid(val): 353 '''validate method''' 354 return (isinstance(val, list) and len(val) == 2 and 355 isinstance(val[0], (int, float)) and -180 <= val[0] <= 180 and 356 isinstance(val[1], (int, float)) and -180 <= val[1] <= 180)
validate method
358 @staticmethod 359 def pointstr_valid(val): 360 '''validate method''' 361 if not isinstance(val, str): 362 return False 363 coord = val.split(',', maxsplit=1) 364 if len(coord) != 2: 365 return False 366 try: 367 point = [float(coord[0]), float(coord[1])] 368 except ValueError: 369 return False 370 return Validator.point_valid(point)
validate method
372 @staticmethod 373 def pointobj_valid(val): 374 '''validate method''' 375 if not (isinstance(val, dict) and 'lon' in val and 'lat' in val): 376 return False 377 return Validator.point_valid([val['lon'], val['lat']])
validate method
379 @staticmethod 380 def multipoint_valid(val): 381 '''validate method''' 382 if not isinstance(val, list): 383 return False 384 for point in val: 385 if not Validator.point_valid(point): 386 return False 387 return True
validate method
389 @staticmethod 390 def line_valid(val): 391 '''validate method''' 392 return Validator.multipoint_valid(val)
validate method
394 @staticmethod 395 def multiline_valid(val): 396 '''validate method''' 397 if not isinstance(val, list): 398 return False 399 for line in val: 400 if not Validator.multipoint_valid(line): 401 return False 402 return True
validate method
404 @staticmethod 405 def polygon_valid(val): 406 '''validate method''' 407 return Validator.multiline_valid(val)
validate method
409 @staticmethod 410 def multipolygon_valid(val): 411 '''validate method''' 412 if not isinstance(val, list): 413 return False 414 for poly in val: 415 if not Validator.multiline_valid(poly): 416 return False 417 return True
validate method
419 @staticmethod 420 def geometry_valid(val): 421 '''validate method''' 422 return (Validator.point_valid(val) or Validator.line_valid(val) or 423 Validator.polygon_valid(val))
validate method
425 @staticmethod 426 def multigeometry_valid(val): 427 '''validate method''' 428 if not isinstance(val, list): 429 return False 430 for geo in val: 431 if not Validator.geometry_valid(geo): 432 return False 433 return True
validate method
435 @staticmethod 436 def box_valid(val): 437 '''validate method''' 438 if not (isinstance(val, list) and len(val) == 4): 439 return False 440 for coor in val: 441 if not (isinstance(coor, (int, float)) and -90 <= coor <= 90): 442 return False 443 return True
validate method
445 @staticmethod 446 def geojson_valid(val): 447 '''validate method''' 448 if not (isinstance(val, dict) and 'type' in val): 449 return False 450 return val['type'] in GEOJSON and GEOJSON[val['type']] in val
validate method
452 @staticmethod 453 def codeolc_valid(val): 454 '''validate method''' 455 if not isinstance(val, str): 456 return False 457 return OLC.fullmatch(val) is not None
validate method
459 @staticmethod 460 def loc_valid(val): 461 '''validate method''' 462 return (Validator.point_valid(val) or Validator.pointstr_valid(val) or 463 Validator.pointobj_valid(val) or Validator.line_valid(val) or 464 Validator.polygon_valid(val) or Validator.multipolygon_valid(val) or 465 Validator.box_valid(val) or Validator.geojson_valid(val) or 466 Validator.codeolc_valid(val))
validate method
468 @staticmethod 469 def unit_valid(val): 470 '''validate method''' 471 return isinstance(val, str)
validate method
473 @staticmethod 474 def uri_valid(val): 475 '''validate method''' 476 if not isinstance(val, str): 477 return False 478 return URI.fullmatch(val) is not None
validate method
480 @staticmethod 481 def uriref_valid(val): 482 '''validate method''' 483 if not isinstance(val, str): 484 return False 485 return URI.fullmatch(val) is not None
validate method
487 @staticmethod 488 def iri_valid(val): 489 '''validate method''' 490 if not isinstance(val, str): 491 return False 492 return URI.fullmatch(val) is not None
validate method
494 @staticmethod 495 def iriref_valid(val): 496 '''validate method''' 497 if not isinstance(val, str): 498 return False 499 return URI.fullmatch(val) is not None
validate method
501 @staticmethod 502 def uritem_valid(val): 503 '''validate method''' 504 if not isinstance(val, str): 505 return False 506 return URI.fullmatch(val) is not None
validate method
508 @staticmethod 509 def uuid_valid(val): 510 '''validate method''' 511 if not isinstance(val, str): 512 return False 513 return UUID.fullmatch(val) is not None
validate method
515 @staticmethod 516 def email_valid(val): 517 '''validate method''' 518 if not isinstance(val, str): 519 return False 520 return ADDRESS.fullmatch(val) is not None
validate method
522 @staticmethod 523 def hostname_valid(val): 524 '''validate method''' 525 if not isinstance(val, str) or len(val) > 253: 526 return False 527 return HOSTNAME.fullmatch(val) is not None
validate method
529 @staticmethod 530 def jpointer_valid(val): 531 '''validate method''' 532 if not isinstance(val, str) or (len(val) > 0 and val[0] != '/'): 533 return False 534 return True
validate method
536 @staticmethod 537 def ipv4_valid(val): 538 '''validate method''' 539 if not isinstance(val, str): 540 return False 541 return IPV4.fullmatch(val) is not None
validate method
543 @staticmethod 544 def file_valid(val): 545 '''validate method''' 546 if not isinstance(val, str): 547 return False 548 return FILE.fullmatch(val) is not None
validate method
Validator exception
Inherited Members
- builtins.Exception
- Exception
- builtins.BaseException
- with_traceback
- add_note
- args