diff --git a/Dockerfile b/Dockerfile index bf8b8bd..0abf1b6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ FROM circleci/python:3.6-jessie -RUN sudo pip install openapi-spec-validator +RUN sudo pip install openapi-spec-validator ansicolors COPY validator.py /opt/ diff --git a/validator.py b/validator.py index 3273b3f..24cb4ed 100644 --- a/validator.py +++ b/validator.py @@ -1,10 +1,70 @@ import sys +from jsonschema.exceptions import RefResolutionError +from colors import color from os import path, getcwd -from openapi_spec_validator import validate_spec_url +from openapi_spec_validator.handlers import UrlHandler +from openapi_spec_validator import openapi_v3_spec_validator def validate(file_name): - validate_spec_url('file://' + path.join(getcwd(), file_name)) + counter = 0 + + try: + handler = UrlHandler('file') + url = 'file://' + path.join(getcwd(), file_name) + 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: + 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 + + +def print_error(count, path, message, instance): + print() + print( + color('Error #%d in [%s]:' % (count, path or 'unknown'), style='bold') + ) + print(" %s" % message) + print(" %s" % instance) def help(): @@ -17,7 +77,7 @@ def main(argv): help() sys.exit(2) - validate(argv[0]) + sys.exit(validate(argv[0])) if __name__ == "__main__":