Skip to content

Commit

Permalink
Merge pull request #2 from usabilla/better-reporting
Browse files Browse the repository at this point in the history
Improve output of validation
  • Loading branch information
lcobucci authored Oct 16, 2017
2 parents 503643c + 4306c0e commit 88f4345
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 5 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/
68 changes: 64 additions & 4 deletions validator.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,83 @@
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():
print('usage: ' + path.basename(__file__) + ' <spec>')


def main(argv):
if (len(argv) != 1):
if len(argv) == 0:
print('Invalid usage!')
help()
sys.exit(2)

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


if __name__ == "__main__":
Expand Down

0 comments on commit 88f4345

Please sign in to comment.