Skip to content

Commit a1c57fc

Browse files
committed
validator read write pass with evolve
1 parent 7e636bd commit a1c57fc

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

openapi_schema_validator/validators.py

+8
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,21 @@ def _patch_validator_with_read_write_context(cls: Type[Validator]) -> None:
8787
# be part of their public API and will raise error
8888
# See https://github.com/p1c2u/openapi-schema-validator/issues/48
8989
original_init = cls.__init__
90+
original_evolve = cls.evolve
9091

9192
def __init__(self: Validator, *args: Any, **kwargs: Any) -> None:
9293
self.read = kwargs.pop("read", None)
9394
self.write = kwargs.pop("write", None)
9495
original_init(self, *args, **kwargs)
9596

97+
def evolve(self: Validator, **changes: Any) -> Validator:
98+
validator = original_evolve(self, **changes)
99+
validator.read = self.read
100+
validator.write = self.write
101+
return validator
102+
96103
cls.__init__ = __init__
104+
cls.evolve = evolve
97105

98106

99107
_patch_validator_with_read_write_context(OAS30Validator)

tests/integration/test_validators.py

+36
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,42 @@ def test_required(self):
225225
validator.validate({"another_prop": "bla"})
226226
assert validator.validate({"some_prop": "hello"}) is None
227227

228+
def test_read_only(self):
229+
schema = {
230+
"type": "object",
231+
"properties": {"some_prop": {"type": "string", "readOnly": True}},
232+
}
233+
234+
validator = OAS30Validator(
235+
schema, format_checker=oas30_format_checker, write=True
236+
)
237+
with pytest.raises(
238+
ValidationError, match="Tried to write read-only property with hello"
239+
):
240+
validator.validate({"some_prop": "hello"})
241+
validator = OAS30Validator(
242+
schema, format_checker=oas30_format_checker, read=True
243+
)
244+
assert validator.validate({"some_prop": "hello"}) is None
245+
246+
def test_write_only(self):
247+
schema = {
248+
"type": "object",
249+
"properties": {"some_prop": {"type": "string", "writeOnly": True}},
250+
}
251+
252+
validator = OAS30Validator(
253+
schema, format_checker=oas30_format_checker, read=True
254+
)
255+
with pytest.raises(
256+
ValidationError, match="Tried to read write-only property with hello"
257+
):
258+
validator.validate({"some_prop": "hello"})
259+
validator = OAS30Validator(
260+
schema, format_checker=oas30_format_checker, write=True
261+
)
262+
assert validator.validate({"some_prop": "hello"}) is None
263+
228264
def test_required_read_only(self):
229265
schema = {
230266
"type": "object",

0 commit comments

Comments
 (0)