Skip to content

Commit 0948b84

Browse files
committed
Added support for v2 and v3
1 parent 933e149 commit 0948b84

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

openapi_spec_validator/__main__.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import os
44
import sys
55

6-
from openapi_spec_validator import validate_spec_url
6+
from openapi_spec_validator import validate_spec_url, validate_v2_spec_url
77
from openapi_spec_validator.exceptions import ValidationError
88

99
logger = logging.getLogger(__name__)
@@ -16,11 +16,24 @@
1616
def main(args=None):
1717
parser = argparse.ArgumentParser()
1818
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+
)
1926
args = parser.parse_args(args)
2027
filename = args.filename
2128
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
2235
try:
23-
validate_spec_url('file://'+filename)
36+
validate_url('file://'+filename)
2437
except ValidationError as e:
2538
print(e)
2639
sys.exit(1)

tests/integration/test_main.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,31 @@
33
from openapi_spec_validator.__main__ import main
44

55

6-
def test_happy_path():
7-
"""No errors when calling with proper file."""
6+
def test_schema_default():
7+
"""Test default schema is 3.0.0"""
88
testargs = ['./tests/integration/data/v3.0/petstore.yaml']
99
main(testargs)
1010

1111

12+
def test_schema_v3():
13+
"""No errors when calling proper v3 file."""
14+
testargs = ['--schema', '3.0.0', './tests/integration/data/v3.0/petstore.yaml']
15+
main(testargs)
16+
17+
18+
def test_schema_v2():
19+
"""No errors when calling with proper v2 file."""
20+
testargs = ['--schema', '2.0', './tests/integration/data/v2.0/petstore.yaml']
21+
main(testargs)
22+
23+
24+
def test_schema_unknown():
25+
"""Errors on running with unknown schema."""
26+
testargs = ['--schema', 'x.x', './tests/integration/data/v2.0/petstore.yaml']
27+
with pytest.raises(SystemExit):
28+
main(testargs)
29+
30+
1231
def test_nonexisting_file():
1332
"""Calling with non-existing file should sys.exit."""
1433
testargs = ['i_dont_exist.yaml']

0 commit comments

Comments
 (0)