|
1 | 1 | import binascii
|
2 | 2 | from base64 import b64decode
|
3 | 3 | from base64 import b64encode
|
4 |
| -from datetime import datetime |
5 | 4 | from typing import Any
|
6 |
| -from typing import Tuple |
7 | 5 | from typing import Union
|
8 |
| -from uuid import UUID |
9 | 6 |
|
10 | 7 | from jsonschema._format import FormatChecker
|
11 |
| -from jsonschema.exceptions import FormatError |
12 |
| - |
13 |
| -DATETIME_HAS_RFC3339_VALIDATOR = False |
14 |
| -DATETIME_HAS_STRICT_RFC3339 = False |
15 |
| -DATETIME_HAS_ISODATE = False |
16 |
| -DATETIME_RAISES: Tuple[Exception, ...] = () |
17 |
| - |
18 |
| -try: |
19 |
| - import isodate |
20 |
| -except ImportError: |
21 |
| - pass |
22 |
| -else: |
23 |
| - DATETIME_HAS_ISODATE = True |
24 |
| - DATETIME_RAISES += (ValueError, isodate.ISO8601Error) |
25 |
| - |
26 |
| -try: |
27 |
| - from rfc3339_validator import validate_rfc3339 |
28 |
| -except ImportError: |
29 |
| - pass |
30 |
| -else: |
31 |
| - DATETIME_HAS_RFC3339_VALIDATOR = True |
32 |
| - DATETIME_RAISES += (ValueError, TypeError) |
33 |
| - |
34 |
| -try: |
35 |
| - import strict_rfc3339 |
36 |
| -except ImportError: |
37 |
| - pass |
38 |
| -else: |
39 |
| - DATETIME_HAS_STRICT_RFC3339 = True |
40 |
| - DATETIME_RAISES += (ValueError, TypeError) |
41 | 8 |
|
42 | 9 |
|
43 | 10 | def is_int32(instance: Any) -> bool:
|
@@ -65,91 +32,32 @@ def is_binary(instance: Any) -> bool:
|
65 | 32 | def is_byte(instance: Union[str, bytes]) -> bool:
|
66 | 33 | if isinstance(instance, str):
|
67 | 34 | instance = instance.encode()
|
68 |
| - |
69 |
| - try: |
70 |
| - encoded = b64encode(b64decode(instance)) |
71 |
| - except TypeError: |
72 |
| - return False |
73 |
| - else: |
74 |
| - return encoded == instance |
75 |
| - |
76 |
| - |
77 |
| -def is_datetime(instance: str) -> bool: |
78 |
| - if not isinstance(instance, (bytes, str)): |
79 |
| - return False |
80 |
| - |
81 |
| - if DATETIME_HAS_RFC3339_VALIDATOR: |
82 |
| - return bool(validate_rfc3339(instance)) |
83 |
| - |
84 |
| - if DATETIME_HAS_STRICT_RFC3339: |
85 |
| - return bool(strict_rfc3339.validate_rfc3339(instance)) |
86 |
| - |
87 |
| - if DATETIME_HAS_ISODATE: |
88 |
| - return bool(isodate.parse_datetime(instance)) |
89 |
| - |
90 |
| - return True |
91 |
| - |
92 |
| - |
93 |
| -def is_date(instance: Any) -> bool: |
94 |
| - if not isinstance(instance, (bytes, str)): |
| 35 | + if not isinstance(instance, bytes): |
95 | 36 | return False
|
96 | 37 |
|
97 |
| - if isinstance(instance, bytes): |
98 |
| - instance = instance.decode() |
99 |
| - |
100 |
| - return bool(datetime.strptime(instance, "%Y-%m-%d")) |
| 38 | + encoded = b64encode(b64decode(instance)) |
| 39 | + return encoded == instance |
101 | 40 |
|
102 | 41 |
|
103 |
| -def is_uuid(instance: Any) -> bool: |
| 42 | +def is_password(instance: Any) -> bool: |
104 | 43 | if not isinstance(instance, (bytes, str)):
|
105 | 44 | return False
|
106 | 45 |
|
107 |
| - if isinstance(instance, bytes): |
108 |
| - instance = instance.decode() |
109 |
| - |
110 |
| - return str(UUID(instance)).lower() == instance.lower() |
111 |
| - |
112 |
| - |
113 |
| -def is_password(instance: Any) -> bool: |
114 | 46 | return True
|
115 | 47 |
|
116 | 48 |
|
117 |
| -class OASFormatChecker(FormatChecker): # type: ignore |
118 |
| - |
119 |
| - checkers = { |
120 |
| - "int32": (is_int32, ()), |
121 |
| - "int64": (is_int64, ()), |
122 |
| - "float": (is_float, ()), |
123 |
| - "double": (is_double, ()), |
124 |
| - "byte": (is_byte, (binascii.Error, TypeError)), |
125 |
| - "binary": (is_binary, ()), |
126 |
| - "date": (is_date, (ValueError,)), |
127 |
| - "date-time": (is_datetime, DATETIME_RAISES), |
128 |
| - "password": (is_password, ()), |
129 |
| - # non standard |
130 |
| - "uuid": (is_uuid, (AttributeError, ValueError)), |
131 |
| - } |
132 |
| - |
133 |
| - def check(self, instance: Any, format: str) -> Any: |
134 |
| - if format not in self.checkers: |
135 |
| - raise FormatError( |
136 |
| - f"Format checker for {format!r} format not found" |
137 |
| - ) |
138 |
| - |
139 |
| - func, raises = self.checkers[format] |
140 |
| - result, cause = None, None |
141 |
| - try: |
142 |
| - result = func(instance) |
143 |
| - except raises as e: # type: ignore |
144 |
| - cause = e |
145 |
| - |
146 |
| - if not result: |
147 |
| - raise FormatError( |
148 |
| - f"{instance!r} is not a {format!r}", |
149 |
| - cause=cause, |
150 |
| - ) |
151 |
| - return result |
152 |
| - |
153 |
| - |
154 |
| -oas30_format_checker = OASFormatChecker() |
155 |
| -oas31_format_checker = oas30_format_checker |
| 49 | +oas30_format_checker = FormatChecker() |
| 50 | +oas30_format_checker.checks("int32")(is_int32) |
| 51 | +oas30_format_checker.checks("int64")(is_int64) |
| 52 | +oas30_format_checker.checks("float")(is_float) |
| 53 | +oas30_format_checker.checks("double")(is_double) |
| 54 | +oas30_format_checker.checks("binary")(is_binary) |
| 55 | +oas30_format_checker.checks("byte", (binascii.Error, TypeError))(is_byte) |
| 56 | +oas30_format_checker.checks("password")(is_password) |
| 57 | + |
| 58 | +oas31_format_checker = FormatChecker() |
| 59 | +oas31_format_checker.checks("int32")(is_int32) |
| 60 | +oas31_format_checker.checks("int64")(is_int64) |
| 61 | +oas31_format_checker.checks("float")(is_float) |
| 62 | +oas31_format_checker.checks("double")(is_double) |
| 63 | +oas31_format_checker.checks("password")(is_password) |
0 commit comments