Skip to content

Commit

Permalink
Merge pull request #166 from p1c2u/refactor/schemas-and-validation-re…
Browse files Browse the repository at this point in the history
…factor

schemas and validation refactor
  • Loading branch information
p1c2u authored Sep 1, 2022
2 parents d047747 + 70325f6 commit dfe4ccb
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 45 deletions.
37 changes: 4 additions & 33 deletions openapi_spec_validator/__init__.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
# -*- coding: utf-8 -*-
from jsonschema.validators import Draft202012Validator
from jsonschema.validators import Draft4Validator
from jsonschema_spec.handlers import default_handlers

from openapi_schema_validator.validators import OAS30Validator
from openapi_schema_validator.validators import OAS31Validator
from openapi_spec_validator.shortcuts import (
validate_spec_factory, validate_spec_url_factory,
)
from openapi_spec_validator.schemas import get_openapi_schema
from openapi_spec_validator.validators import SpecValidator
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
from openapi_spec_validator.validation import openapi_v31_spec_validator

__author__ = 'Artur Maciag'
__email__ = '[email protected]'
Expand All @@ -34,30 +32,6 @@
'validate_spec_url',
]

# v2.0 spec
schema_v2, _ = get_openapi_schema('2.0')
openapi_v2_schema_validator = Draft4Validator(schema_v2)
openapi_v2_spec_validator = SpecValidator(
openapi_v2_schema_validator, OAS30Validator,
resolver_handlers=default_handlers,
)

# v3.0 spec
schema_v30, _ = get_openapi_schema('3.0')
openapi_v30_schema_validator = Draft4Validator(schema_v30)
openapi_v30_spec_validator = SpecValidator(
openapi_v30_schema_validator, OAS30Validator,
resolver_handlers=default_handlers,
)

# v3.1 spec
schema_v31, _ = get_openapi_schema('3.1')
openapi_v31_schema_validator = Draft202012Validator(schema_v31)
openapi_v31_spec_validator = SpecValidator(
openapi_v31_schema_validator, OAS31Validator,
resolver_handlers=default_handlers,
)

# shortcuts
validate_v2_spec = validate_spec_factory(openapi_v2_spec_validator.validate)
validate_v2_spec_url = validate_spec_url_factory(
Expand All @@ -67,14 +41,11 @@
validate_v30_spec_url = validate_spec_url_factory(
openapi_v30_spec_validator.validate, default_handlers)


validate_v31_spec = validate_spec_factory(openapi_v31_spec_validator.validate)
validate_v31_spec_url = validate_spec_url_factory(
openapi_v31_spec_validator.validate, default_handlers)

# aliases to the latest v3 version
schema_v3 = schema_v31
openapi_v3_spec_validator = openapi_v31_spec_validator
validate_v3_spec = validate_v31_spec
validate_v3_spec_url = validate_v31_spec_url

Expand Down
2 changes: 1 addition & 1 deletion openapi_spec_validator/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
openapi_v30_spec_validator,
openapi_v31_spec_validator,
)
from openapi_spec_validator.exceptions import ValidationError
from openapi_spec_validator.validation.exceptions import ValidationError
from openapi_spec_validator.readers import read_from_stdin, read_from_filename

logger = logging.getLogger(__name__)
Expand Down
11 changes: 11 additions & 0 deletions openapi_spec_validator/schemas/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"""OpenAIP spec validator schemas module."""
from openapi_spec_validator.schemas.utils import get_schema

__all__ = ["schema_v2", "schema_v3", "schema_v30", "schema_v31"]

schema_v2, _ = get_schema('2.0')
schema_v30, _ = get_schema('3.0')
schema_v31, _ = get_schema('3.1')

# alias to the latest v3 version
schema_v3 = schema_v31
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"""OpenAIP spec validator schemas module."""
"""OpenAIP spec validator schemas utils module."""
from os import path

import importlib_resources

from jsonschema_spec.readers import FilePathReader


def get_openapi_schema(version):
def get_schema(version):
schema_path = 'resources/schemas/v{0}/schema.json'.format(version)
ref = importlib_resources.files('openapi_spec_validator') / schema_path
with importlib_resources.as_file(ref) as resource_path:
Expand Down
44 changes: 44 additions & 0 deletions openapi_spec_validator/validation/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# -*- coding: utf-8 -*-
from jsonschema.validators import Draft202012Validator
from jsonschema.validators import Draft4Validator
from jsonschema_spec.handlers import default_handlers
from openapi_schema_validator import oas30_format_checker
from openapi_schema_validator import oas31_format_checker
from openapi_schema_validator.validators import OAS30Validator
from openapi_schema_validator.validators import OAS31Validator

from openapi_spec_validator.schemas import schema_v2
from openapi_spec_validator.schemas import schema_v30
from openapi_spec_validator.schemas import schema_v31
from openapi_spec_validator.validation.validators import SpecValidator

__all__ = [
'openapi_v2_spec_validator',
'openapi_v3_spec_validator',
'openapi_v30_spec_validator',
'openapi_v31_spec_validator',
]

# v2.0 spec
openapi_v2_schema_validator = Draft4Validator(schema_v2)
openapi_v2_spec_validator = SpecValidator(
openapi_v2_schema_validator, OAS30Validator, oas30_format_checker,
resolver_handlers=default_handlers,
)

# v3.0 spec
openapi_v30_schema_validator = Draft4Validator(schema_v30)
openapi_v30_spec_validator = SpecValidator(
openapi_v30_schema_validator, OAS30Validator, oas30_format_checker,
resolver_handlers=default_handlers,
)

# v3.1 spec
openapi_v31_schema_validator = Draft202012Validator(schema_v31)
openapi_v31_spec_validator = SpecValidator(
openapi_v31_schema_validator, OAS31Validator, oas31_format_checker,
resolver_handlers=default_handlers,
)

# alias to the latest v3 version
openapi_v3_spec_validator = openapi_v31_spec_validator
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""OpenAPI spec validator decorators module."""
"""OpenAPI spec validator validation decorators module."""
from functools import wraps
import logging

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
"""OpenAPI spec validator validation validators module."""
import logging
import string

from jsonschema.validators import RefResolver
from jsonschema_spec.accessors import SpecAccessor
from jsonschema_spec.paths import Spec
from openapi_schema_validator import OAS31Validator, oas31_format_checker

from openapi_spec_validator.exceptions import (
from openapi_spec_validator.validation.exceptions import (
ParameterDuplicateError, ExtraParametersError, UnresolvableParameterError,
OpenAPIValidationError, DuplicateOperationIDError,
)
from openapi_spec_validator.decorators import ValidationErrorWrapper
from openapi_spec_validator.validation.decorators import ValidationErrorWrapper

log = logging.getLogger(__name__)

Expand All @@ -27,9 +27,10 @@ class SpecValidator(object):
'get', 'put', 'post', 'delete', 'options', 'head', 'patch', 'trace',
]

def __init__(self, schema_validator, value_validator_class, resolver_handlers=None):
def __init__(self, schema_validator, value_validator_class, value_validator_format_checker, resolver_handlers=None):
self.schema_validator = schema_validator
self.value_validator_class = value_validator_class
self.value_validator_format_checker = value_validator_format_checker
self.resolver_handlers = resolver_handlers

self.operation_ids_registry = None
Expand Down Expand Up @@ -141,7 +142,7 @@ def _iter_value_errors(self, schema, value):
validator = self.value_validator_class(
content,
resolver=self.resolver,
format_checker=oas31_format_checker,
format_checker=self.value_validator_format_checker,
)
yield from validator.iter_errors(value)

Expand Down
2 changes: 1 addition & 1 deletion tests/integration/test_errors.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from openapi_spec_validator.exceptions import (
from openapi_spec_validator.validation.exceptions import (
ExtraParametersError, UnresolvableParameterError, OpenAPIValidationError,
DuplicateOperationIDError,
)
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/test_shortcuts.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
openapi_v2_spec_validator, openapi_v30_spec_validator,
validate_v30_spec_url, validate_v30_spec,
)
from openapi_spec_validator.exceptions import OpenAPIValidationError
from openapi_spec_validator.validation.exceptions import OpenAPIValidationError


class TestLocalOpenAPIv20Validator:
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/test_validators.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

from openapi_spec_validator.exceptions import OpenAPIValidationError
from openapi_spec_validator.validation.exceptions import OpenAPIValidationError


class TestLocalOpenAPIv30Validator:
Expand Down

0 comments on commit dfe4ccb

Please sign in to comment.