-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Nick Timkovich
committed
Aug 8, 2018
1 parent
7e27cad
commit 1e552ea
Showing
2 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Swagger spec validator. | ||
""" | ||
from __future__ import absolute_import | ||
from __future__ import division | ||
from __future__ import print_function | ||
from __future__ import unicode_literals | ||
|
||
|
||
import argparse | ||
import sys | ||
|
||
from swagger_spec_validator.common import SwaggerValidationError | ||
from swagger_spec_validator.util import validate_spec_url | ||
|
||
|
||
def _extract_error_message(exc): | ||
""" | ||
The validators return nested errors coming from common.wrap_exception, but | ||
other errors like invalid URLs aren't nested. Try to | ||
""" | ||
try: | ||
return exc.args[1].args[0] | ||
except IndexError: | ||
pass | ||
|
||
return exc.args[0] | ||
|
||
|
||
def main(): | ||
parser = argparse.ArgumentParser(description=__doc__) | ||
|
||
parser.add_argument("url", type=str, help="URL of Swagger spec to validate") | ||
parser.add_argument( | ||
"-q", | ||
"--quiet", | ||
action="store_true", | ||
help="No output if validation is successful", | ||
) | ||
|
||
args = parser.parse_args() | ||
|
||
try: | ||
validate_spec_url(args.url) | ||
except SwaggerValidationError as exc: | ||
error_message = _extract_error_message(exc) | ||
print(error_message, file=sys.stderr) | ||
return 1 | ||
|
||
if not args.quiet: | ||
print("Validation successful!", file=sys.stderr) | ||
|
||
return 0 | ||
|
||
|
||
if __name__ == "__main__": | ||
sys.exit(main()) |