Skip to content

Commit

Permalink
Validator accept any schema
Browse files Browse the repository at this point in the history
  • Loading branch information
p1c2u committed Oct 13, 2023
1 parent e450e39 commit eeff41a
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 19 deletions.
6 changes: 6 additions & 0 deletions openapi_spec_validator/schemas/types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from typing import Union

from jsonschema_path.paths import SchemaPath
from jsonschema_path.typing import Schema

AnySchema = Union[Schema, SchemaPath]
26 changes: 13 additions & 13 deletions openapi_spec_validator/validation/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from openapi_spec_validator.schemas import openapi_v2_schema_validator
from openapi_spec_validator.schemas import openapi_v30_schema_validator
from openapi_spec_validator.schemas import openapi_v31_schema_validator
from openapi_spec_validator.schemas.types import AnySchema
from openapi_spec_validator.validation import keywords
from openapi_spec_validator.validation.decorators import unwraps_iter
from openapi_spec_validator.validation.decorators import wraps_cached_iter
Expand All @@ -39,11 +40,10 @@ class SpecValidator:

def __init__(
self,
schema: Schema,
schema: AnySchema,
base_uri: str = "",
spec_url: Optional[str] = None,
) -> None:
self.schema = schema
if spec_url is not None:
warnings.warn(
"spec_url parameter is deprecated. " "Use base_uri instead.",
Expand All @@ -52,11 +52,16 @@ def __init__(
base_uri = spec_url
self.base_uri = base_uri

self.spec = SchemaPath.from_dict(
self.schema,
base_uri=self.base_uri,
handlers=self.resolver_handlers,
)
if isinstance(schema, SchemaPath):
self.schema_path = schema
self.schema = schema.contents()
else:
self.schema = schema
self.schema_path = SchemaPath.from_dict(
self.schema,
base_uri=self.base_uri,
handlers=self.resolver_handlers,
)

self.keyword_validators_registry = KeywordValidatorRegistry(
self.keyword_validators
Expand Down Expand Up @@ -84,12 +89,7 @@ def root_validator(self) -> keywords.RootValidator:
def iter_errors(self) -> Iterator[ValidationError]:
yield from self.schema_validator.iter_errors(self.schema)

spec = SchemaPath.from_dict(
self.schema,
base_uri=self.base_uri,
handlers=self.resolver_handlers,
)
yield from self.root_validator(spec)
yield from self.root_validator(self.schema_path)


class OpenAPIV2SpecValidator(SpecValidator):
Expand Down
8 changes: 4 additions & 4 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ jsonschema = "^4.18.0"
openapi-schema-validator = "^0.6.0"
python = "^3.8.0"
importlib-resources = {version = ">=5.8,<7.0", python = "<3.9" }
jsonschema-path = "^0.3.0"
jsonschema-path = "^0.3.1"
lazy-object-proxy = "^1.7.1"

[tool.poetry.extras]
Expand Down
7 changes: 6 additions & 1 deletion tests/integration/validation/test_validators.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest
from referencing.exceptions import Unresolvable

from jsonschema_path import SchemaPath
from openapi_spec_validator import OpenAPIV2SpecValidator
from openapi_spec_validator import OpenAPIV30SpecValidator
from openapi_spec_validator import OpenAPIV31SpecValidator
Expand All @@ -23,7 +24,11 @@ def test_valid(self, factory, spec_file):
spec_path = self.local_test_suite_file_path(spec_file)
spec = factory.spec_from_file(spec_path)
spec_url = factory.spec_file_url(spec_path)
validator = OpenAPIV2SpecValidator(spec, base_uri=spec_url)
schema_path = SchemaPath.from_dict(
spec,
base_uri=spec_url,
)
validator = OpenAPIV2SpecValidator(schema_path)

validator.validate()

Expand Down

0 comments on commit eeff41a

Please sign in to comment.