11import pytest
22from jsonschema import ValidationError
33
4+ from openapi_schema_validator import OAS30ReadValidator
5+ from openapi_schema_validator import OAS30WriteValidator
46from openapi_schema_validator import OAS30Validator
57from openapi_schema_validator import OAS31Validator
68from openapi_schema_validator import oas30_format_checker
@@ -258,16 +260,18 @@ def test_read_only(self):
258260 "properties" : {"some_prop" : {"type" : "string" , "readOnly" : True }},
259261 }
260262
261- validator = OAS30Validator (
262- schema , format_checker = oas30_format_checker , write = True
263- )
263+ with pytest .warns (DeprecationWarning ):
264+ validator = OAS30Validator (
265+ schema , format_checker = oas30_format_checker , write = True
266+ )
264267 with pytest .raises (
265268 ValidationError , match = "Tried to write read-only property with hello"
266269 ):
267270 validator .validate ({"some_prop" : "hello" })
268- validator = OAS30Validator (
269- schema , format_checker = oas30_format_checker , read = True
270- )
271+ with pytest .warns (DeprecationWarning ):
272+ validator = OAS30Validator (
273+ schema , format_checker = oas30_format_checker , read = True
274+ )
271275 assert validator .validate ({"some_prop" : "hello" }) is None
272276
273277 def test_write_only (self ):
@@ -276,16 +280,18 @@ def test_write_only(self):
276280 "properties" : {"some_prop" : {"type" : "string" , "writeOnly" : True }},
277281 }
278282
279- validator = OAS30Validator (
280- schema , format_checker = oas30_format_checker , read = True
281- )
283+ with pytest .warns (DeprecationWarning ):
284+ validator = OAS30Validator (
285+ schema , format_checker = oas30_format_checker , read = True
286+ )
282287 with pytest .raises (
283288 ValidationError , match = "Tried to read write-only property with hello"
284289 ):
285290 validator .validate ({"some_prop" : "hello" })
286- validator = OAS30Validator (
287- schema , format_checker = oas30_format_checker , write = True
288- )
291+ with pytest .warns (DeprecationWarning ):
292+ validator = OAS30Validator (
293+ schema , format_checker = oas30_format_checker , write = True
294+ )
289295 assert validator .validate ({"some_prop" : "hello" }) is None
290296
291297 def test_required_read_only (self ):
@@ -295,16 +301,18 @@ def test_required_read_only(self):
295301 "required" : ["some_prop" ],
296302 }
297303
298- validator = OAS30Validator (
299- schema , format_checker = oas30_format_checker , read = True
300- )
304+ with pytest .warns (DeprecationWarning ):
305+ validator = OAS30Validator (
306+ schema , format_checker = oas30_format_checker , read = True
307+ )
301308 with pytest .raises (
302309 ValidationError , match = "'some_prop' is a required property"
303310 ):
304311 validator .validate ({"another_prop" : "hello" })
305- validator = OAS30Validator (
306- schema , format_checker = oas30_format_checker , write = True
307- )
312+ with pytest .warns (DeprecationWarning ):
313+ validator = OAS30Validator (
314+ schema , format_checker = oas30_format_checker , write = True
315+ )
308316 assert validator .validate ({"another_prop" : "hello" }) is None
309317
310318 def test_required_write_only (self ):
@@ -314,16 +322,18 @@ def test_required_write_only(self):
314322 "required" : ["some_prop" ],
315323 }
316324
317- validator = OAS30Validator (
318- schema , format_checker = oas30_format_checker , write = True
319- )
325+ with pytest .warns (DeprecationWarning ):
326+ validator = OAS30Validator (
327+ schema , format_checker = oas30_format_checker , write = True
328+ )
320329 with pytest .raises (
321330 ValidationError , match = "'some_prop' is a required property"
322331 ):
323332 validator .validate ({"another_prop" : "hello" })
324- validator = OAS30Validator (
325- schema , format_checker = oas30_format_checker , read = True
326- )
333+ with pytest .warns (DeprecationWarning ):
334+ validator = OAS30Validator (
335+ schema , format_checker = oas30_format_checker , read = True
336+ )
327337 assert validator .validate ({"another_prop" : "hello" }) is None
328338
329339 def test_oneof_required (self ):
@@ -567,6 +577,84 @@ def test_nullable_schema_combos(self, is_nullable, schema_type, not_nullable_reg
567577 validator .validate ({"testfield" : None })
568578 assert False
569579
580+
581+ class TestOAS30ReadWriteValidatorValidate :
582+
583+ def test_read_only (self ):
584+ schema = {
585+ "type" : "object" ,
586+ "properties" : {"some_prop" : {"type" : "string" , "readOnly" : True }},
587+ }
588+
589+ validator = OAS30WriteValidator (
590+ schema , format_checker = oas30_format_checker ,
591+ )
592+ with pytest .raises (
593+ ValidationError , match = "Tried to write read-only property with hello"
594+ ):
595+ validator .validate ({"some_prop" : "hello" })
596+ validator = OAS30ReadValidator (
597+ schema , format_checker = oas30_format_checker ,
598+ )
599+ assert validator .validate ({"some_prop" : "hello" }) is None
600+
601+ def test_write_only (self ):
602+ schema = {
603+ "type" : "object" ,
604+ "properties" : {"some_prop" : {"type" : "string" , "writeOnly" : True }},
605+ }
606+
607+ validator = OAS30ReadValidator (
608+ schema , format_checker = oas30_format_checker ,
609+ )
610+ with pytest .raises (
611+ ValidationError , match = "Tried to read write-only property with hello"
612+ ):
613+ validator .validate ({"some_prop" : "hello" })
614+ validator = OAS30WriteValidator (
615+ schema , format_checker = oas30_format_checker ,
616+ )
617+ assert validator .validate ({"some_prop" : "hello" }) is None
618+
619+ def test_required_read_only (self ):
620+ schema = {
621+ "type" : "object" ,
622+ "properties" : {"some_prop" : {"type" : "string" , "readOnly" : True }},
623+ "required" : ["some_prop" ],
624+ }
625+
626+ validator = OAS30ReadValidator (
627+ schema , format_checker = oas30_format_checker ,
628+ )
629+ with pytest .raises (
630+ ValidationError , match = "'some_prop' is a required property"
631+ ):
632+ validator .validate ({"another_prop" : "hello" })
633+ validator = OAS30WriteValidator (
634+ schema , format_checker = oas30_format_checker ,
635+ )
636+ assert validator .validate ({"another_prop" : "hello" }) is None
637+
638+ def test_required_write_only (self ):
639+ schema = {
640+ "type" : "object" ,
641+ "properties" : {"some_prop" : {"type" : "string" , "writeOnly" : True }},
642+ "required" : ["some_prop" ],
643+ }
644+
645+ validator = OAS30WriteValidator (
646+ schema , format_checker = oas30_format_checker ,
647+ )
648+ with pytest .raises (
649+ ValidationError , match = "'some_prop' is a required property"
650+ ):
651+ validator .validate ({"another_prop" : "hello" })
652+ validator = OAS30ReadValidator (
653+ schema , format_checker = oas30_format_checker ,
654+ )
655+ assert validator .validate ({"another_prop" : "hello" }) is None
656+
657+
570658class TestOAS31ValidatorValidate :
571659 @pytest .mark .parametrize (
572660 "schema_type" ,
0 commit comments