55
66from jsonschema ._format import FormatChecker
77
8+ from openapi_schema_validator ._regex import is_valid_regex
9+
810
911def is_int32 (instance : object ) -> bool :
1012 # bool inherits from int, so ensure bools aren't reported as ints
@@ -82,6 +84,12 @@ def is_password(instance: object) -> bool:
8284 return True
8385
8486
87+ def is_regex (instance : object ) -> bool :
88+ if not isinstance (instance , str ):
89+ return True
90+ return is_valid_regex (instance )
91+
92+
8593oas30_format_checker = FormatChecker ()
8694oas30_format_checker .checks ("int32" )(is_int32 )
8795oas30_format_checker .checks ("int64" )(is_int64 )
@@ -90,6 +98,7 @@ def is_password(instance: object) -> bool:
9098oas30_format_checker .checks ("binary" )(is_binary_pragmatic )
9199oas30_format_checker .checks ("byte" , (binascii .Error , TypeError ))(is_byte )
92100oas30_format_checker .checks ("password" )(is_password )
101+ oas30_format_checker .checks ("regex" )(is_regex )
93102
94103oas30_strict_format_checker = FormatChecker ()
95104oas30_strict_format_checker .checks ("int32" )(is_int32 )
@@ -101,13 +110,15 @@ def is_password(instance: object) -> bool:
101110 is_byte
102111)
103112oas30_strict_format_checker .checks ("password" )(is_password )
113+ oas30_strict_format_checker .checks ("regex" )(is_regex )
104114
105115oas31_format_checker = FormatChecker ()
106116oas31_format_checker .checks ("int32" )(is_int32 )
107117oas31_format_checker .checks ("int64" )(is_int64 )
108118oas31_format_checker .checks ("float" )(is_float )
109119oas31_format_checker .checks ("double" )(is_double )
110120oas31_format_checker .checks ("password" )(is_password )
121+ oas31_format_checker .checks ("regex" )(is_regex )
111122
112123# OAS 3.2 uses the same format checks as OAS 3.1
113124oas32_format_checker = FormatChecker ()
@@ -116,3 +127,4 @@ def is_password(instance: object) -> bool:
116127oas32_format_checker .checks ("float" )(is_float )
117128oas32_format_checker .checks ("double" )(is_double )
118129oas32_format_checker .checks ("password" )(is_password )
130+ oas32_format_checker .checks ("regex" )(is_regex )
0 commit comments