def validate(url): counter = 0 try: handler = UrlHandler("http", "https", "file") if not urlparse(url).scheme: url = "file://" + path.join(getcwd(), url) spec = handler(url) for i in openapi_v3_spec_validator.iter_errors(spec, spec_url=url): counter += 1 print_error(counter, ":".join(i.absolute_path), i.message, i.instance) except RefResolutionError as e: counter += 1 print_error( counter, "", f"Unable to resolve {e.__context__.args[0]} in {e.args[0]}", "", ) except BaseException: counter += 1 print_error(counter, "", sys.exc_info()[0], "") finally: if counter > 0: print() print( color( " [FAIL] %d errors found " % counter, fg="white", bg="red", style="bold", )) return 1 else: print_ok(" [PASS] No errors found ") return 0
def validate(url): counter = 0 try: handler = UrlHandler('http', 'https', 'file') if not urlparse(url).scheme: url = 'file://' + path.join(getcwd(), url) spec = handler(url) for i in openapi_v3_spec_validator.iter_errors(spec, spec_url=url): counter += 1 print_error(counter, ':'.join(i.absolute_path), i.message, i.instance) except RefResolutionError as e: counter += 1 print_error( counter, '', f'Unable to resolve {e.__context__.args[0]} in {e.args[0]}', '') except BaseException: counter += 1 print_error(counter, '', sys.exc_info()[0], '') finally: if counter > 0: print() print( color(' [FAIL] %d errors found ' % counter, fg='white', bg='red', style='bold')) return 1 else: print( color(' [PASS] No errors found ', fg='white', bg='green', style='bold')) return 0
) from openapi_spec_validator.handlers import UrlHandler from openapi_spec_validator.schemas import get_openapi_schema from openapi_spec_validator.factories import JSONSpecValidatorFactory from openapi_spec_validator.validators import SpecValidator __author__ = 'Artur Maciąg' __email__ = '*****@*****.**' __version__ = '0.1.0' __url__ = 'https://github.com/p1c2u/openapi-spec-validator' __license__ = 'Apache License, Version 2.0' __all__ = ['openapi_v3_validator', 'validate_spec', 'validate_spec_url'] default_handlers = { '<all_urls>': UrlHandler('http', 'https', 'file'), 'http': UrlHandler('http'), 'https': UrlHandler('https'), 'file': UrlHandler('file'), } schema_v3, schema_v3_url = get_openapi_schema('3.0.0') openapi_v3_validator_factory = JSONSpecValidatorFactory( schema_v3, schema_v3_url, resolver_handlers=default_handlers, ) openapi_v3_spec_validator = SpecValidator( openapi_v3_validator_factory, resolver_handlers=default_handlers, ) # shortcuts validate_spec = validate_spec_factory(openapi_v3_spec_validator.validate)
import collections from copy import deepcopy from jsonschema import Draft4Validator, RefResolver, _utils from jsonschema.exceptions import RefResolutionError, ValidationError # noqa from jsonschema.validators import extend from openapi_spec_validator.handlers import UrlHandler from .utils import deep_get default_handlers = { 'http': UrlHandler('http'), 'https': UrlHandler('https'), 'file': UrlHandler('file'), } def resolve_refs(spec, store=None, handlers=None): """ Resolve JSON references like {"$ref": <some URI>} in a spec. Optionally takes a store, which is a mapping from reference URLs to a dereferenced objects. Prepopulating the store can avoid network calls. """ spec = deepcopy(spec) store = store or {} handlers = handlers or default_handlers resolver = RefResolver('', spec, store, handlers=handlers) def _do_resolve(node): if isinstance(node, collections.Mapping) and '$ref' in node: path = node['$ref'][2:].split("/")
def load_spec(url): """Return OpenAPI specification dictionary for given URL""" handler = UrlHandler('http', 'https', 'file') if not urlparse(url).scheme: url = 'file://' + os.path.join(os.getcwd(), url) return handler(url), url
'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', ] file_object_handler = FileObjectHandler() all_urls_handler = UrlHandler('http', 'https', 'file') default_handlers = { '<all_urls>': all_urls_handler, 'http': UrlHandler('http'), 'https': UrlHandler('https'), 'file': UrlHandler('file'), } # v2.0 spec schema_v2, schema_v2_url = get_openapi_schema('2.0') openapi_v2_validator_factory = Draft4JSONSpecValidatorFactory( schema_v2, schema_v2_url, resolver_handlers=default_handlers, ) openapi_v2_spec_validator = SpecValidator(