Skip to content

readOnly and writeOnly on jsonschema4 fix #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions openapi_schema_validator/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ def __init__(self, *args, **kwargs):
self.write = kwargs.pop('write', None)
super(OAS30Validator, self).__init__(*args, **kwargs)

def evolve(self, **kwargs):
# jsonschema4 interface compatibility workaround
validator = super(OAS30Validator, self).evolve(**kwargs)
validator.read = self.read
validator.write = self.write
return validator

def iter_errors(self, instance, _schema=None):
if _schema is None:
# creates a copy by value from schema to prevent mutation
Expand Down
6 changes: 6 additions & 0 deletions tests/integration/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@ def test_required_read_only(self):
validator.validate({"another_prop": "hello"})
validator = OAS30Validator(schema, format_checker=oas30_format_checker,
write=True)
with pytest.raises(ValidationError,
match="Tried to write read-only property with hello"):
validator.validate({"some_prop": "hello"})
assert validator.validate({"another_prop": "hello"}) is None

def test_required_write_only(self):
Expand All @@ -217,6 +220,9 @@ def test_required_write_only(self):
validator.validate({"another_prop": "hello"})
validator = OAS30Validator(schema, format_checker=oas30_format_checker,
read=True)
with pytest.raises(ValidationError,
match="Tried to read write-only property with hello"):
validator.validate({"some_prop": "hello"})
assert validator.validate({"another_prop": "hello"}) is None

def test_oneof_required(self):
Expand Down