From 49ad680fb90a34e841dac4dcddbc59411e087323 Mon Sep 17 00:00:00 2001 From: vyuroshchin Date: Mon, 10 Mar 2025 01:14:59 +0300 Subject: [PATCH 1/2] improve logging in shortcuts.py and turn to pytest in test_shortcut.py --- docs/index.rst | 2 +- openapi_schema_validator/shortcuts.py | 8 ++--- openapi_schema_validator/validators.py | 1 - tests/unit/test_shortcut.py | 42 +++++++++++++++++--------- 4 files changed, 32 insertions(+), 21 deletions(-) diff --git a/docs/index.rst b/docs/index.rst index 1b9b6fe..f89daba 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -20,7 +20,7 @@ Installation .. md-tab-set:: - .. md-tab-item:: Pip + PyPI (recommented) + .. md-tab-item:: Pip + PyPI (recommended) .. code-block:: console diff --git a/openapi_schema_validator/shortcuts.py b/openapi_schema_validator/shortcuts.py index f6e940d..b080b65 100644 --- a/openapi_schema_validator/shortcuts.py +++ b/openapi_schema_validator/shortcuts.py @@ -18,8 +18,8 @@ def validate( schema_dict = cast(dict[str, Any], schema) cls.check_schema(schema_dict) validator = cls(schema_dict, *args, **kwargs) - error = best_match( - validator.evolve(schema=schema_dict).iter_errors(instance) - ) - if error is not None: + errors = list(validator.evolve(schema=schema_dict).iter_errors(instance)) + if errors: + error = best_match(errors) + error.message = f"Validation failed: {error.message}" raise error diff --git a/openapi_schema_validator/validators.py b/openapi_schema_validator/validators.py index 00e6458..647d44b 100644 --- a/openapi_schema_validator/validators.py +++ b/openapi_schema_validator/validators.py @@ -1,6 +1,5 @@ from typing import Any from typing import cast - from jsonschema import _keywords from jsonschema import _legacy_keywords from jsonschema.validators import Draft202012Validator diff --git a/tests/unit/test_shortcut.py b/tests/unit/test_shortcut.py index 0ec80c7..0ceaa3e 100644 --- a/tests/unit/test_shortcut.py +++ b/tests/unit/test_shortcut.py @@ -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": "foo@bar.com"}, - } + }, + "example": {"enabled": False, "email": "foo@bar.com"}, + } + + +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": "foo@bar.com"}, schema) + assert "nullable" not in schema["properties"]["email"].keys() - validate({"email": "foo@bar.com"}, 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": "foo@bar.com"}, schema) + assert schema == original_schema From 35e7b4aebdeeefa956e175567095ebde2bbc7632 Mon Sep 17 00:00:00 2001 From: vyuroshchin Date: Sat, 22 Mar 2025 18:43:28 +0300 Subject: [PATCH 2/2] fix review --- openapi_schema_validator/shortcuts.py | 11 +++++++---- openapi_schema_validator/validators.py | 1 + tests/unit/test_shortcut.py | 1 + 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/openapi_schema_validator/shortcuts.py b/openapi_schema_validator/shortcuts.py index b080b65..b9d60aa 100644 --- a/openapi_schema_validator/shortcuts.py +++ b/openapi_schema_validator/shortcuts.py @@ -15,11 +15,14 @@ def validate( *args: Any, **kwargs: Any ) -> None: + """ + Validate an instance against a given schema using the specified validator class. + """ schema_dict = cast(dict[str, Any], schema) cls.check_schema(schema_dict) validator = cls(schema_dict, *args, **kwargs) - errors = list(validator.evolve(schema=schema_dict).iter_errors(instance)) - if errors: - error = best_match(errors) - error.message = f"Validation failed: {error.message}" + error = best_match( + validator.evolve(schema=schema_dict).iter_errors(instance) + ) + if error is not None: raise error diff --git a/openapi_schema_validator/validators.py b/openapi_schema_validator/validators.py index 647d44b..00e6458 100644 --- a/openapi_schema_validator/validators.py +++ b/openapi_schema_validator/validators.py @@ -1,5 +1,6 @@ from typing import Any from typing import cast + from jsonschema import _keywords from jsonschema import _legacy_keywords from jsonschema.validators import Draft202012Validator diff --git a/tests/unit/test_shortcut.py b/tests/unit/test_shortcut.py index 0ceaa3e..26eca30 100644 --- a/tests/unit/test_shortcut.py +++ b/tests/unit/test_shortcut.py @@ -1,4 +1,5 @@ import pytest + from openapi_schema_validator import validate