1515from referencing .jsonschema import DRAFT202012
1616
1717from openapi_schema_validator import OAS30ReadValidator
18+ from openapi_schema_validator import OAS30StrictValidator
1819from openapi_schema_validator import OAS30Validator
1920from openapi_schema_validator import OAS30WriteValidator
2021from openapi_schema_validator import OAS31Validator
2122from openapi_schema_validator import oas30_format_checker
23+ from openapi_schema_validator import oas30_strict_format_checker
2224from openapi_schema_validator import oas31_format_checker
2325
2426
@@ -187,7 +189,6 @@ def test_oas30_formats_ignored(
187189
188190 assert result is None
189191
190- @pytest .mark .xfail (reason = "OAS 3.0 string type checker allows byte" )
191192 @pytest .mark .parametrize ("value" , [b"test" ])
192193 def test_string_disallow_binary (self , validator_class , value ):
193194 schema = {"type" : "string" }
@@ -205,7 +206,7 @@ def test_string_binary_valid(self, validator_class, format_checker, value):
205206
206207 assert result is None
207208
208- @pytest .mark .parametrize ("value" , ["test" , True , 3 , 3.12 , None ])
209+ @pytest .mark .parametrize ("value" , [True , 3 , 3.12 , None ])
209210 def test_string_binary_invalid (
210211 self , validator_class , format_checker , value
211212 ):
@@ -282,7 +283,6 @@ def test_nullable_enum_with_none(self, validator_class):
282283 @pytest .mark .parametrize (
283284 "value" ,
284285 [
285- b64encode (b"string" ),
286286 b64encode (b"string" ).decode (),
287287 ],
288288 )
@@ -296,7 +296,7 @@ def test_string_format_byte_valid(self, validator_class, value):
296296
297297 assert result is None
298298
299- @pytest .mark .parametrize ("value" , ["string" , b"string" ])
299+ @pytest .mark .parametrize ("value" , ["string" ])
300300 def test_string_format_byte_invalid (self , validator_class , value ):
301301 schema = {"type" : "string" , "format" : "byte" }
302302 validator = validator_class (
@@ -1001,3 +1001,53 @@ def test_array_prefixitems_invalid(self, validator_class, value):
10011001 "Expected at most 4 items but found 1 extra" ,
10021002 ]
10031003 assert any (error in str (excinfo .value ) for error in errors )
1004+
1005+
1006+ class TestOAS30StrictValidator :
1007+ """
1008+ Tests for OAS30StrictValidator which follows OAS spec strictly:
1009+ - type: string only accepts str (not bytes)
1010+ - format: binary also only accepts str (no special bytes handling)
1011+ """
1012+
1013+ def test_strict_string_rejects_bytes (self ):
1014+ """Strict validator rejects bytes for plain string type."""
1015+ schema = {"type" : "string" }
1016+ validator = OAS30StrictValidator (schema )
1017+
1018+ with pytest .raises (ValidationError ):
1019+ validator .validate (b"test" )
1020+
1021+ def test_strict_string_accepts_str (self ):
1022+ """Strict validator accepts str for string type."""
1023+ schema = {"type" : "string" }
1024+ validator = OAS30StrictValidator (schema )
1025+
1026+ result = validator .validate ("test" )
1027+ assert result is None
1028+
1029+ def test_strict_binary_format_rejects_bytes (self ):
1030+ """Strict validator rejects bytes even with binary format."""
1031+ schema = {"type" : "string" , "format" : "binary" }
1032+ validator = OAS30StrictValidator (
1033+ schema , format_checker = oas30_format_checker
1034+ )
1035+
1036+ with pytest .raises (ValidationError ):
1037+ validator .validate (b"test" )
1038+
1039+ def test_strict_binary_format_rejects_str (self ):
1040+ """
1041+ Strict validator with binary format rejects strings.
1042+ Binary format is for bytes in OAS, not plain strings.
1043+ """
1044+ schema = {"type" : "string" , "format" : "binary" }
1045+ validator = OAS30StrictValidator (
1046+ schema , format_checker = oas30_strict_format_checker
1047+ )
1048+
1049+ # Binary format expects actual binary data (bytes in Python)
1050+ # Plain strings fail format validation because they are not valid base64
1051+ # Note: "test" is actually valid base64, so use "not base64" which is not
1052+ with pytest .raises (ValidationError , match = "is not a 'binary'" ):
1053+ validator .validate ("not base64" )
0 commit comments