Skip to content

Commit

Permalink
Merge pull request #172 from p1c2u/refactor/shortcuts-refactor-and-va…
Browse files Browse the repository at this point in the history
…lidators-explicit-usage

Shortcuts refactor and validators explicit usage
  • Loading branch information
p1c2u authored Sep 2, 2022
2 parents 1ed28ff + 000a15d commit ded5eff
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 80 deletions.
24 changes: 9 additions & 15 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,6 @@ By default, OpenAPI spec version is detected. To validate spec:
...
OpenAPIValidationError: 'info' is a required property
In order to explicitly validate a:

* Swagger / OpenAPI 2.0 spec, import ``validate_v2_spec``
* OpenAPI 3.0 spec, import ``validate_v30_spec``
* OpenAPI 3.1 spec, import ``validate_v31_spec``

instead of ``validate_spec``.

You can also explicitly import ``validate_v3_spec`` which is a shortcut to the latest v3 release.

Add ``spec_url`` to validate spec with relative files:

.. code:: python
Expand All @@ -117,13 +107,17 @@ You can also validate spec from url:
In order to explicitly validate a:

* Swagger / OpenAPI 2.0 spec url, import ``validate_v2_spec_url``
* OpenAPI 3.0 spec url, import ``validate_v30_spec_url``
* OpenAPI 3.1 spec url, import ``validate_v31_spec_url``
* Swagger / OpenAPI 2.0 spec, import ``openapi_v2_spec_validator``
* OpenAPI 3.0 spec, import ``openapi_v30_spec_validator``
* OpenAPI 3.1 spec, import ``openapi_v31_spec_validator``

and pass the validator to ``validate_spec`` or ``validate_spec_url`` function:

.. code:: python
instead of ``validate_spec_url``.
validate_spec(spec_dict, validator=openapi_v31_spec_validator)
You can also explicitly import ``validate_v3_spec_url`` which is a shortcut to the latest v3 release.
You can also explicitly import ``openapi_v3_spec_validator`` which is a shortcut to the latest v3 release.

If you want to iterate through validation errors:

Expand Down
30 changes: 2 additions & 28 deletions openapi_spec_validator/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from jsonschema_spec.handlers import default_handlers

from openapi_spec_validator.shortcuts import validate_spec_factory
from openapi_spec_validator.shortcuts import validate_spec_url_factory
from openapi_spec_validator.validation import openapi_spec_validator_proxy
from openapi_spec_validator.shortcuts import validate_spec
from openapi_spec_validator.shortcuts import validate_spec_url
from openapi_spec_validator.validation import openapi_v2_spec_validator
from openapi_spec_validator.validation import openapi_v3_spec_validator
from openapi_spec_validator.validation import openapi_v30_spec_validator
Expand All @@ -19,31 +18,6 @@
"openapi_v3_spec_validator",
"openapi_v30_spec_validator",
"openapi_v31_spec_validator",
"validate_v2_spec",
"validate_v3_spec",
"validate_v30_spec",
"validate_v31_spec",
"validate_spec",
"validate_v2_spec_url",
"validate_v3_spec_url",
"validate_v30_spec_url",
"validate_v31_spec_url",
"validate_spec_url",
]

# shortcuts
validate_v2_spec = validate_spec_factory(openapi_v2_spec_validator)
validate_v2_spec_url = validate_spec_url_factory(openapi_v2_spec_validator)

validate_v30_spec = validate_spec_factory(openapi_v30_spec_validator)
validate_v30_spec_url = validate_spec_url_factory(openapi_v30_spec_validator)

validate_v31_spec = validate_spec_factory(openapi_v31_spec_validator)
validate_v31_spec_url = validate_spec_url_factory(openapi_v31_spec_validator)

validate_spec = validate_spec_factory(openapi_spec_validator_proxy)
validate_spec_url = validate_spec_url_factory(openapi_spec_validator_proxy)

# aliases to the latest v3 version
validate_v3_spec = validate_v31_spec
validate_v3_spec_url = validate_v31_spec_url
29 changes: 13 additions & 16 deletions openapi_spec_validator/shortcuts.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
"""OpenAPI spec validator shortcuts module."""
from typing import Any
from typing import Callable
from typing import Hashable
from typing import Mapping

from jsonschema_spec.handlers import all_urls_handler

from openapi_spec_validator.validation import openapi_spec_validator_proxy
from openapi_spec_validator.validation.protocols import SupportsValidation


def validate_spec_factory(
validator: SupportsValidation,
) -> Callable[[Mapping[Hashable, Any], str], None]:
def validate(spec: Mapping[Hashable, Any], spec_url: str = "") -> None:
return validator.validate(spec, spec_url=spec_url)
def validate_spec(
spec: Mapping[Hashable, Any],
spec_url: str = "",
validator: SupportsValidation = openapi_spec_validator_proxy,
) -> None:
return validator.validate(spec, spec_url=spec_url)

return validate


def validate_spec_url_factory(
validator: SupportsValidation,
) -> Callable[[str], None]:
def validate(spec_url: str) -> None:
spec = all_urls_handler(spec_url)
return validator.validate(spec, spec_url=spec_url)

return validate
def validate_spec_url(
spec_url: str,
validator: SupportsValidation = openapi_spec_validator_proxy,
) -> None:
spec = all_urls_handler(spec_url)
return validator.validate(spec, spec_url=spec_url)
1 change: 0 additions & 1 deletion openapi_spec_validator/validation/proxies.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@


class DetectValidatorProxy:

def __init__(self, choices: Mapping[Tuple[str, str], SpecValidator]):
self.choices = choices

Expand Down
27 changes: 7 additions & 20 deletions tests/integration/test_shortcuts.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,7 @@
from openapi_spec_validator import openapi_v2_spec_validator
from openapi_spec_validator import openapi_v30_spec_validator
from openapi_spec_validator import validate_spec
from openapi_spec_validator import validate_spec_factory
from openapi_spec_validator import validate_spec_url
from openapi_spec_validator import validate_spec_url_factory
from openapi_spec_validator import validate_v2_spec
from openapi_spec_validator import validate_v2_spec_url
from openapi_spec_validator import validate_v30_spec
from openapi_spec_validator import validate_v30_spec_url
from openapi_spec_validator.validation.exceptions import OpenAPIValidationError
from openapi_spec_validator.validation.exceptions import ValidatorDetectError

Expand Down Expand Up @@ -50,9 +44,7 @@ def test_valid(self, factory, spec_file):
spec_url = factory.spec_file_url(spec_path)

validate_spec(spec)
validate_v2_spec(spec)

validate_spec_factory(openapi_v2_spec_validator)(spec, spec_url)
validate_spec(spec, validator=openapi_v2_spec_validator)

@pytest.mark.parametrize(
"spec_file",
Expand All @@ -65,7 +57,7 @@ def test_falied(self, factory, spec_file):
spec = factory.spec_from_file(spec_path)

with pytest.raises(OpenAPIValidationError):
validate_v2_spec(spec)
validate_spec(spec, validator=openapi_v2_spec_validator)


class TestLocalValidatev30Spec:
Expand All @@ -87,9 +79,8 @@ def test_valid(self, factory, spec_file):
spec_url = factory.spec_file_url(spec_path)

validate_spec(spec)
validate_v30_spec(spec)

validate_spec_factory(openapi_v30_spec_validator)(spec, spec_url)
validate_spec(spec, spec_url=spec_url)
validate_spec(spec, validator=openapi_v30_spec_validator)

@pytest.mark.parametrize(
"spec_file",
Expand All @@ -102,7 +93,7 @@ def test_falied(self, factory, spec_file):
spec = factory.spec_from_file(spec_path)

with pytest.raises(OpenAPIValidationError):
validate_v30_spec(spec)
validate_spec(spec, validator=openapi_v30_spec_validator)


@pytest.mark.network
Expand Down Expand Up @@ -130,9 +121,7 @@ def test_valid(self, spec_file):
spec_url = self.remote_test_suite_file_path(spec_file)

validate_spec_url(spec_url)
validate_v2_spec_url(spec_url)

validate_spec_url_factory(openapi_v2_spec_validator)(spec_url)
validate_spec_url(spec_url, validator=openapi_v2_spec_validator)


@pytest.mark.network
Expand Down Expand Up @@ -160,6 +149,4 @@ def test_valid(self, spec_file):
spec_url = self.remote_test_suite_file_path(spec_file)

validate_spec_url(spec_url)
validate_v30_spec_url(spec_url)

validate_spec_url_factory(openapi_v30_spec_validator)(spec_url)
validate_spec_url(spec_url, validator=openapi_v30_spec_validator)

0 comments on commit ded5eff

Please sign in to comment.