Skip to content

Update SupportsValidation protocol #232

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

Merged
merged 1 commit into from
Jun 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions openapi_spec_validator/shortcuts.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import Any
from typing import Hashable
from typing import Mapping
from typing import Optional

from jsonschema_spec.handlers import all_urls_handler

Expand All @@ -11,15 +12,16 @@

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


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)
return validator.validate(spec, base_uri=spec_url)
11 changes: 9 additions & 2 deletions openapi_spec_validator/validation/protocols.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import Hashable
from typing import Iterator
from typing import Mapping
from typing import Optional
from typing import Protocol
from typing import runtime_checkable

Expand All @@ -14,11 +15,17 @@ def is_valid(self, instance: Mapping[Hashable, Any]) -> bool:
...

def iter_errors(
self, instance: Mapping[Hashable, Any], spec_url: str = ""
self,
instance: Mapping[Hashable, Any],
base_uri: str = "",
spec_url: Optional[str] = None,
) -> Iterator[OpenAPIValidationError]:
...

def validate(
self, instance: Mapping[Hashable, Any], spec_url: str = ""
self,
instance: Mapping[Hashable, Any],
base_uri: str = "",
spec_url: Optional[str] = None,
) -> None:
...
19 changes: 15 additions & 4 deletions openapi_spec_validator/validation/proxies.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import Hashable
from typing import Iterator
from typing import Mapping
from typing import Optional
from typing import Tuple

from openapi_spec_validator.validation.exceptions import OpenAPIValidationError
Expand All @@ -21,10 +22,15 @@ def detect(self, instance: Mapping[Hashable, Any]) -> SpecValidator:
raise ValidatorDetectError("Spec schema version not detected")

def validate(
self, instance: Mapping[Hashable, Any], spec_url: str = ""
self,
instance: Mapping[Hashable, Any],
base_uri: str = "",
spec_url: Optional[str] = None,
) -> None:
validator = self.detect(instance)
for err in validator.iter_errors(instance, spec_url=spec_url):
for err in validator.iter_errors(
instance, base_uri=base_uri, spec_url=spec_url
):
raise err

def is_valid(self, instance: Mapping[Hashable, Any]) -> bool:
Expand All @@ -33,7 +39,12 @@ def is_valid(self, instance: Mapping[Hashable, Any]) -> bool:
return error is None

def iter_errors(
self, instance: Mapping[Hashable, Any], spec_url: str = ""
self,
instance: Mapping[Hashable, Any],
base_uri: str = "",
spec_url: Optional[str] = None,
) -> Iterator[OpenAPIValidationError]:
validator = self.detect(instance)
yield from validator.iter_errors(instance, spec_url=spec_url)
yield from validator.iter_errors(
instance, base_uri=base_uri, spec_url=spec_url
)