Skip to content

Commit

Permalink
Improve output of validation
Browse files Browse the repository at this point in the history
Made the output more redable by listing the errors, being less verbose
and using colors to signal success/failure.
  • Loading branch information
rdohms authored and Luís Cobucci committed Oct 16, 2017
1 parent 6de2487 commit 4306c0e
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -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/
66 changes: 63 additions & 3 deletions validator.py
Original file line number Diff line number Diff line change
@@ -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():
Expand All @@ -17,7 +77,7 @@ def main(argv):
help()
sys.exit(2)

validate(argv[0])
sys.exit(validate(argv[0]))


if __name__ == "__main__":
Expand Down

0 comments on commit 4306c0e

Please sign in to comment.