Skip to content

Commit 8a91bfb

Browse files
authored
Merge pull request #16 from sebastianmika/fix_required_in_composition
Fix required in composition
2 parents ab7222a + b106d9c commit 8a91bfb

File tree

2 files changed

+101
-6
lines changed

2 files changed

+101
-6
lines changed

Diff for: openapi_schema_validator/_validators.py

+9-6
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

Diff for: tests/integration/test_validators.py

+92
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,95 @@ 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_required(self):
165+
schema = {
166+
"type": "object",
167+
"properties": {
168+
"some_prop": {
169+
"type": "string"
170+
}
171+
},
172+
"required": ["some_prop"]
173+
}
174+
175+
validator = OAS30Validator(schema, format_checker=oas30_format_checker)
176+
with pytest.raises(ValidationError,
177+
match="'some_prop' is a required property"):
178+
validator.validate({"another_prop": "bla"})
179+
assert validator.validate({"some_prop": "hello"}) is None
180+
181+
def test_required_read_only(self):
182+
schema = {
183+
"type": "object",
184+
"properties": {
185+
"some_prop": {
186+
"type": "string",
187+
"readOnly": True
188+
}
189+
},
190+
"required": ["some_prop"]
191+
}
192+
193+
validator = OAS30Validator(schema, format_checker=oas30_format_checker,
194+
read=True)
195+
with pytest.raises(ValidationError,
196+
match="'some_prop' is a required property"):
197+
validator.validate({"another_prop": "hello"})
198+
validator = OAS30Validator(schema, format_checker=oas30_format_checker,
199+
write=True)
200+
assert validator.validate({"another_prop": "hello"}) is None
201+
202+
def test_required_write_only(self):
203+
schema = {
204+
"type": "object",
205+
"properties": {
206+
"some_prop": {
207+
"type": "string",
208+
"writeOnly": True
209+
}
210+
},
211+
"required": ["some_prop"]
212+
}
213+
214+
validator = OAS30Validator(schema, format_checker=oas30_format_checker,
215+
write=True)
216+
with pytest.raises(ValidationError,
217+
match="'some_prop' is a required property"):
218+
validator.validate({"another_prop": "hello"})
219+
validator = OAS30Validator(schema, format_checker=oas30_format_checker,
220+
read=True)
221+
assert validator.validate({"another_prop": "hello"}) is None
222+
223+
def test_oneof_required(self):
224+
instance = {
225+
'n3IwfId': 'string',
226+
}
227+
schema = {
228+
"type": "object",
229+
"properties": {
230+
"n3IwfId": {"type": "string"},
231+
"wagfId": {"type": "string"},
232+
},
233+
"oneOf": [
234+
{"required": ["n3IwfId"]},
235+
{"required": ["wagfId"]},
236+
],
237+
}
238+
validator = OAS30Validator(schema, format_checker=oas30_format_checker)
239+
result = validator.validate(instance)
240+
assert result is None

0 commit comments

Comments
 (0)