Skip to content

Commit 9e60855

Browse files
authored
Merge pull request #9 from venthur/feat/entry_points
Added CLI interface for ad-hoc validation
2 parents cba42bb + 690bc2d commit 9e60855

File tree

4 files changed

+105
-0
lines changed

4 files changed

+105
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ OpenAPI Spec Validator is a Python library that validates OpenAPI Specs against
1717

1818
## Usage
1919

20+
Command Line Interface:
21+
22+
```bash
23+
$ openapi_spec_validator some.yaml
24+
```
25+
2026
Validate spec:
2127

2228
```python

openapi_spec_validator/__main__.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import logging
2+
import argparse
3+
import os
4+
import sys
5+
6+
from openapi_spec_validator import validate_spec_url, validate_v2_spec_url
7+
from openapi_spec_validator.exceptions import ValidationError
8+
9+
logger = logging.getLogger(__name__)
10+
logging.basicConfig(
11+
format='%(asctime)s %(levelname)s %(name)s %(message)s',
12+
level=logging.WARNING
13+
)
14+
15+
16+
def main(args=None):
17+
parser = argparse.ArgumentParser()
18+
parser.add_argument('filename', help="Absolute or relative path to file")
19+
parser.add_argument(
20+
'--schema',
21+
help="OpenAPI schema (default: 3.0.0)",
22+
type=str,
23+
choices=['2.0', '3.0.0'],
24+
default='3.0.0'
25+
)
26+
args = parser.parse_args(args)
27+
filename = args.filename
28+
filename = os.path.abspath(filename)
29+
# choose the validator
30+
if args.schema == '2.0':
31+
validate_url = validate_v2_spec_url
32+
elif args.schema == '3.0.0':
33+
validate_url = validate_spec_url
34+
# validate
35+
try:
36+
validate_url('file://'+filename)
37+
except ValidationError as e:
38+
print(e)
39+
sys.exit(1)
40+
except Exception as e:
41+
print(e)
42+
sys.exit(2)
43+
else:
44+
print('OK')
45+
46+
47+
if __name__ == '__main__':
48+
main()

setup.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ def run_tests(self):
6161
],
6262
},
6363
include_package_data=True,
64+
entry_points={
65+
'console_scripts': [
66+
'openapi-spec-validator = openapi_spec_validator.__main__:main'
67+
]
68+
},
6469
install_requires=[
6570
"jsonschema",
6671
"pyaml",

tests/integration/test_main.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import pytest
2+
3+
from openapi_spec_validator.__main__ import main
4+
5+
6+
def test_schema_default():
7+
"""Test default schema is 3.0.0"""
8+
testargs = ['./tests/integration/data/v3.0/petstore.yaml']
9+
main(testargs)
10+
11+
12+
def test_schema_v3():
13+
"""No errors when calling proper v3 file."""
14+
testargs = ['--schema', '3.0.0',
15+
'./tests/integration/data/v3.0/petstore.yaml']
16+
main(testargs)
17+
18+
19+
def test_schema_v2():
20+
"""No errors when calling with proper v2 file."""
21+
testargs = ['--schema', '2.0',
22+
'./tests/integration/data/v2.0/petstore.yaml']
23+
main(testargs)
24+
25+
26+
def test_schema_unknown():
27+
"""Errors on running with unknown schema."""
28+
testargs = ['--schema', 'x.x',
29+
'./tests/integration/data/v2.0/petstore.yaml']
30+
with pytest.raises(SystemExit):
31+
main(testargs)
32+
33+
34+
def test_validation_error():
35+
"""SystemExit on running with ValidationError."""
36+
testargs = ['--schema', '3.0.0',
37+
'./tests/integration/data/v2.0/petstore.yaml']
38+
with pytest.raises(SystemExit):
39+
main(testargs)
40+
41+
42+
def test_nonexisting_file():
43+
"""Calling with non-existing file should sys.exit."""
44+
testargs = ['i_dont_exist.yaml']
45+
with pytest.raises(SystemExit):
46+
main(testargs)

0 commit comments

Comments
 (0)