Skip to content

Commit 49ad680

Browse files
vyuroshchinp1c2u
authored andcommitted
improve logging in shortcuts.py and turn to pytest in test_shortcut.py
1 parent f585459 commit 49ad680

File tree

4 files changed

+32
-21
lines changed

4 files changed

+32
-21
lines changed

docs/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Installation
2020

2121
.. md-tab-set::
2222

23-
.. md-tab-item:: Pip + PyPI (recommented)
23+
.. md-tab-item:: Pip + PyPI (recommended)
2424

2525
.. code-block:: console
2626

openapi_schema_validator/shortcuts.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ def validate(
1818
schema_dict = cast(dict[str, Any], schema)
1919
cls.check_schema(schema_dict)
2020
validator = cls(schema_dict, *args, **kwargs)
21-
error = best_match(
22-
validator.evolve(schema=schema_dict).iter_errors(instance)
23-
)
24-
if error is not None:
21+
errors = list(validator.evolve(schema=schema_dict).iter_errors(instance))
22+
if errors:
23+
error = best_match(errors)
24+
error.message = f"Validation failed: {error.message}"
2525
raise error

openapi_schema_validator/validators.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from typing import Any
22
from typing import cast
3-
43
from jsonschema import _keywords
54
from jsonschema import _legacy_keywords
65
from jsonschema.validators import Draft202012Validator

tests/unit/test_shortcut.py

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,33 @@
1-
from unittest import TestCase
2-
1+
import pytest
32
from openapi_schema_validator import validate
43

54

6-
class ValidateTest(TestCase):
7-
def test_validate_does_not_mutate_schema_adding_nullable_key(self):
8-
schema = {
9-
"type": "object",
10-
"properties": {
11-
"email": {"type": "string"},
12-
"enabled": {
13-
"type": "boolean",
14-
},
5+
@pytest.fixture(scope="function")
6+
def schema():
7+
return {
8+
"type": "object",
9+
"properties": {
10+
"email": {"type": "string"},
11+
"enabled": {
12+
"type": "boolean",
1513
},
16-
"example": {"enabled": False, "email": "foo@bar.com"},
17-
}
14+
},
15+
"example": {"enabled": False, "email": "foo@bar.com"},
16+
}
17+
18+
19+
def test_validate_does_not_add_nullable_to_schema(schema):
20+
"""
21+
Verify that calling validate does not add the 'nullable' key to the schema
22+
"""
23+
validate({"email": "foo@bar.com"}, schema)
24+
assert "nullable" not in schema["properties"]["email"].keys()
1825

19-
validate({"email": "foo@bar.com"}, schema)
2026

21-
self.assertTrue("nullable" not in schema["properties"]["email"].keys())
27+
def test_validate_does_not_mutate_schema(schema):
28+
"""
29+
Verify that calling validate does not mutate the schema
30+
"""
31+
original_schema = schema.copy()
32+
validate({"email": "foo@bar.com"}, schema)
33+
assert schema == original_schema

0 commit comments

Comments
 (0)