Skip to content

Commit e3f59bf

Browse files
committed
readOnly and writeOnly on jsonschema4
1 parent 92d952f commit e3f59bf

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

openapi_schema_validator/validators.py

+7
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ def __init__(self, *args, **kwargs):
6363
self.write = kwargs.pop('write', None)
6464
super(OAS30Validator, self).__init__(*args, **kwargs)
6565

66+
def evolve(self, **kwargs):
67+
# jsonschema4 interface compatibility workaround
68+
validator = super(OAS30Validator, self).evolve(**kwargs)
69+
validator.read = self.read
70+
validator.write = self.write
71+
return validator
72+
6673
def iter_errors(self, instance, _schema=None):
6774
if _schema is None:
6875
# creates a copy by value from schema to prevent mutation

tests/integration/test_validators.py

+6
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,9 @@ def test_required_read_only(self):
196196
validator.validate({"another_prop": "hello"})
197197
validator = OAS30Validator(schema, format_checker=oas30_format_checker,
198198
write=True)
199+
with pytest.raises(ValidationError,
200+
match="Tried to write read-only property with hello"):
201+
validator.validate({"some_prop": "hello"})
199202
assert validator.validate({"another_prop": "hello"}) is None
200203

201204
def test_required_write_only(self):
@@ -217,6 +220,9 @@ def test_required_write_only(self):
217220
validator.validate({"another_prop": "hello"})
218221
validator = OAS30Validator(schema, format_checker=oas30_format_checker,
219222
read=True)
223+
with pytest.raises(ValidationError,
224+
match="Tried to read write-only property with hello"):
225+
validator.validate({"some_prop": "hello"})
220226
assert validator.validate({"another_prop": "hello"}) is None
221227

222228
def test_oneof_required(self):

0 commit comments

Comments
 (0)