Skip to content

Fix required in composition #16

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
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
15 changes: 9 additions & 6 deletions openapi_schema_validator/_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def format(validator, format, instance, schema):
try:
validator.format_checker.check(instance, format)
except FormatError as error:
yield ValidationError(error.message, cause=error.cause)
yield ValidationError(str(error), cause=error.cause)


def items(validator, items, instance, schema):
Expand All @@ -40,11 +40,14 @@ def required(validator, required, instance, schema):
return
for property in required:
if property not in instance:
prop_schema = schema['properties'][property]
read_only = prop_schema.get('readOnly', False)
write_only = prop_schema.get('writeOnly', False)
if validator.write and read_only or validator.read and write_only:
continue
prop_schema = schema.get('properties', {}).get(property)
if prop_schema:
read_only = prop_schema.get('readOnly', False)
write_only = prop_schema.get('writeOnly', False)
if (
validator.write and read_only or
validator.read and write_only):
continue
yield ValidationError("%r is a required property" % property)


Expand Down
92 changes: 92 additions & 0 deletions tests/integration/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,95 @@ def test_string_uuid(self, value):
result = validator.validate(value)

assert result is None

def test_allof_required(self):
schema = {
"allOf": [
{"type": "object",
"properties": {
"some_prop": {"type": "string"}}},
{"type": "object", "required": ["some_prop"]},
]
}
validator = OAS30Validator(schema, format_checker=oas30_format_checker)
with pytest.raises(ValidationError,
match="'some_prop' is a required property"):
validator.validate({"another_prop": "bla"})

def test_required(self):
schema = {
"type": "object",
"properties": {
"some_prop": {
"type": "string"
}
},
"required": ["some_prop"]
}

validator = OAS30Validator(schema, format_checker=oas30_format_checker)
with pytest.raises(ValidationError,
match="'some_prop' is a required property"):
validator.validate({"another_prop": "bla"})
assert validator.validate({"some_prop": "hello"}) is None

def test_required_read_only(self):
schema = {
"type": "object",
"properties": {
"some_prop": {
"type": "string",
"readOnly": True
}
},
"required": ["some_prop"]
}

validator = OAS30Validator(schema, format_checker=oas30_format_checker,
read=True)
with pytest.raises(ValidationError,
match="'some_prop' is a required property"):
validator.validate({"another_prop": "hello"})
validator = OAS30Validator(schema, format_checker=oas30_format_checker,
write=True)
assert validator.validate({"another_prop": "hello"}) is None

def test_required_write_only(self):
schema = {
"type": "object",
"properties": {
"some_prop": {
"type": "string",
"writeOnly": True
}
},
"required": ["some_prop"]
}

validator = OAS30Validator(schema, format_checker=oas30_format_checker,
write=True)
with pytest.raises(ValidationError,
match="'some_prop' is a required property"):
validator.validate({"another_prop": "hello"})
validator = OAS30Validator(schema, format_checker=oas30_format_checker,
read=True)
assert validator.validate({"another_prop": "hello"}) is None

def test_oneof_required(self):
instance = {
'n3IwfId': 'string',
}
schema = {
"type": "object",
"properties": {
"n3IwfId": {"type": "string"},
"wagfId": {"type": "string"},
},
"oneOf": [
{"required": ["n3IwfId"]},
{"required": ["wagfId"]},
],
}
validator = OAS30Validator(schema, format_checker=oas30_format_checker)
result = validator.validate(instance)
assert result is None