Skip to content

Commit 92df023

Browse files
author
Sebastian Mika
committed
Only validate readOnly/writeOnly for required properties if the property is defined on the same sub-schema as the required
Clean up some flake8 issues
1 parent 185112b commit 92df023

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

openapi_schema_validator/_validators.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def format(validator, format, instance, schema):
1818
try:
1919
validator.format_checker.check(instance, format)
2020
except FormatError as error:
21-
yield ValidationError(error.message, cause=error.cause)
21+
yield ValidationError(str(error), cause=error.cause)
2222

2323

2424
def items(validator, items, instance, schema):
@@ -40,11 +40,14 @@ def required(validator, required, instance, schema):
4040
return
4141
for property in required:
4242
if property not in instance:
43-
prop_schema = schema['properties'][property]
44-
read_only = prop_schema.get('readOnly', False)
45-
write_only = prop_schema.get('writeOnly', False)
46-
if validator.write and read_only or validator.read and write_only:
47-
continue
43+
prop_schema = schema.get('properties', {}).get(property)
44+
if prop_schema:
45+
read_only = prop_schema.get('readOnly', False)
46+
write_only = prop_schema.get('writeOnly', False)
47+
if (
48+
validator.write and read_only or
49+
validator.read and write_only):
50+
continue
4851
yield ValidationError("%r is a required property" % property)
4952

5053

tests/integration/test_validators.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,36 @@ def test_string_uuid(self, value):
146146
result = validator.validate(value)
147147

148148
assert result is None
149+
150+
def test_allof_required(self):
151+
schema = {
152+
"allOf": [
153+
{"type": "object",
154+
"properties": {
155+
"some_prop": {"type": "string"}}},
156+
{"type": "object", "required": ["some_prop"]},
157+
]
158+
}
159+
validator = OAS30Validator(schema, format_checker=oas30_format_checker)
160+
with pytest.raises(ValidationError,
161+
match="'some_prop' is a required property"):
162+
validator.validate({"another_prop": "bla"})
163+
164+
def test_oneof_required(self):
165+
instance = {
166+
'n3IwfId': 'string',
167+
}
168+
schema = {
169+
"type": "object",
170+
"properties": {
171+
"n3IwfId": {"type": "string"},
172+
"wagfId": {"type": "string"},
173+
},
174+
"oneOf": [
175+
{"required": ["n3IwfId"]},
176+
{"required": ["wagfId"]},
177+
],
178+
}
179+
validator = OAS30Validator(schema, format_checker=oas30_format_checker)
180+
result = validator.validate(instance)
181+
assert result is None

0 commit comments

Comments
 (0)