-
Notifications
You must be signed in to change notification settings - Fork 3
/
validator.py
90 lines (74 loc) · 2.22 KB
/
validator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import sys
from os import path, getcwd
from colors import color
from jsonschema.exceptions import RefResolutionError
from openapi_spec_validator import openapi_v3_spec_validator
from openapi_spec_validator.handlers import UrlHandler
from six.moves.urllib.parse import urlparse
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
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)
# For legacy mode only - some existing pipelines override docker image entry point
# and use this validator.py module directly instead of main one
def help():
print('usage: ' + path.basename(__file__) + ' <spec>')
def main(argv):
if len(argv) == 0:
print('Invalid usage!')
help()
sys.exit(2)
sys.exit(validate(argv[0]))
if __name__ == "__main__":
main(sys.argv[1:])