-
-
Notifications
You must be signed in to change notification settings - Fork 32
improve logging in shortcuts.py and turn to pytest in test_shortcut.py #219
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,4 @@ | ||
from typing import Any | ||
from typing import Hashable | ||
from typing import Mapping | ||
from typing import Type | ||
from typing import Any, Hashable, Mapping, Type | ||
|
||
from jsonschema.exceptions import best_match | ||
from jsonschema.protocols import Validator | ||
|
@@ -16,8 +13,24 @@ | |
*args: Any, | ||
**kwargs: Any | ||
) -> None: | ||
""" | ||
Validate an instance against a given schema using the specified validator class | ||
|
||
Args: | ||
instance: The instance to validate. | ||
schema: The schema to validate against | ||
cls: The validator class to use (defaults to OAS31Validator) | ||
*args: Additional positional arguments to pass to the validator | ||
**kwargs: Additional keyword arguments to pass to the validator | ||
|
||
Raises: | ||
jsonschema.exceptions.ValidationError: If the instance is invalid according to the schema | ||
""" | ||
cls.check_schema(schema) | ||
validator = cls(schema, *args, **kwargs) | ||
error = best_match(validator.evolve(schema=schema).iter_errors(instance)) | ||
if error is not None: | ||
errors = list(validator.evolve(schema=schema).iter_errors(instance)) | ||
|
||
if errors: | ||
error = best_match(errors) | ||
error.message = f"Validation failed: {error.message}" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be nice to have it tested There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I studied a little more on my work project. Returned the logic as it was before my edits. |
||
raise error |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,33 @@ | ||
from unittest import TestCase | ||
|
||
import pytest | ||
from openapi_schema_validator import validate | ||
|
||
|
||
class ValidateTest(TestCase): | ||
def test_validate_does_not_mutate_schema_adding_nullable_key(self): | ||
schema = { | ||
"type": "object", | ||
"properties": { | ||
"email": {"type": "string"}, | ||
"enabled": { | ||
"type": "boolean", | ||
}, | ||
@pytest.fixture(scope="function") | ||
def schema(): | ||
return { | ||
"type": "object", | ||
"properties": { | ||
"email": {"type": "string"}, | ||
"enabled": { | ||
"type": "boolean", | ||
}, | ||
"example": {"enabled": False, "email": "[email protected]"}, | ||
} | ||
}, | ||
"example": {"enabled": False, "email": "[email protected]"}, | ||
} | ||
|
||
|
||
def test_validate_does_not_add_nullable_to_schema(schema): | ||
""" | ||
Verify that calling validate does not add the 'nullable' key to the schema | ||
""" | ||
validate({"email": "[email protected]"}, schema) | ||
assert "nullable" not in schema["properties"]["email"].keys() | ||
|
||
validate({"email": "[email protected]"}, schema) | ||
|
||
self.assertTrue("nullable" not in schema["properties"]["email"].keys()) | ||
def test_validate_does_not_mutate_schema(schema): | ||
""" | ||
Verify that calling validate does not mutate the schema | ||
""" | ||
original_schema = schema.copy() | ||
validate({"email": "[email protected]"}, schema) | ||
assert schema == original_schema |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would keep it as it is here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Returned it back