Skip to content

Commit bc25733

Browse files
author
vyuroshchin
committed
improve logging in shortcuts.py and turn to pytest in test_shortcut.py
1 parent 2c7de6d commit bc25733

File tree

5 files changed

+48
-30
lines changed

5 files changed

+48
-30
lines changed

docs/index.rst

+1-1
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/_keywords.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
from copy import deepcopy
21
from typing import Any
3-
from typing import Dict
42
from typing import Hashable
5-
from typing import ItemsView
63
from typing import Iterator
74
from typing import List
85
from typing import Mapping
@@ -112,7 +109,7 @@ def type(
112109
# * nullable: true is only meaningful in combination with a type
113110
# assertion specified in the same Schema Object.
114111
# * nullable: true operates within a single Schema Object
115-
if "nullable" in schema and schema["nullable"] == True:
112+
if "nullable" in schema and schema["nullable"] is True:
116113
return
117114
yield ValidationError("None for not nullable")
118115

openapi_schema_validator/shortcuts.py

+19-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
from typing import Any
2-
from typing import Hashable
3-
from typing import Mapping
4-
from typing import Type
1+
from typing import Any, Hashable, Mapping, Type
52

63
from jsonschema.exceptions import best_match
74
from jsonschema.protocols import Validator
@@ -16,8 +13,24 @@ def validate(
1613
*args: Any,
1714
**kwargs: Any
1815
) -> None:
16+
"""
17+
Validate an instance against a given schema using the specified validator class
18+
19+
Args:
20+
instance: The instance to validate.
21+
schema: The schema to validate against
22+
cls: The validator class to use (defaults to OAS31Validator)
23+
*args: Additional positional arguments to pass to the validator
24+
**kwargs: Additional keyword arguments to pass to the validator
25+
26+
Raises:
27+
jsonschema.exceptions.ValidationError: If the instance is invalid according to the schema
28+
"""
1929
cls.check_schema(schema)
2030
validator = cls(schema, *args, **kwargs)
21-
error = best_match(validator.evolve(schema=schema).iter_errors(instance))
22-
if error is not None:
31+
errors = list(validator.evolve(schema=schema).iter_errors(instance))
32+
33+
if errors:
34+
error = best_match(errors)
35+
error.message = f"Validation failed: {error.message}"
2336
raise error

openapi_schema_validator/validators.py

-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
import warnings
2-
from typing import Any
3-
from typing import Type
4-
51
from jsonschema import _keywords
62
from jsonschema import _legacy_keywords
73
from jsonschema.validators import Draft202012Validator

tests/unit/test_shortcut.py

+27-15
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": "[email protected]"},
17-
}
14+
},
15+
"example": {"enabled": False, "email": "[email protected]"},
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": "[email protected]"}, schema)
24+
assert "nullable" not in schema["properties"]["email"].keys()
1825

19-
validate({"email": "[email protected]"}, 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": "[email protected]"}, schema)
33+
assert schema == original_schema

0 commit comments

Comments
 (0)