Skip to content

Commit d230b67

Browse files
committed
Allow to validate multiple files
1 parent 613c70a commit d230b67

File tree

1 file changed

+49
-36
lines changed

1 file changed

+49
-36
lines changed

openapi_spec_validator/__main__.py

Lines changed: 49 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,18 @@
2121
)
2222

2323

24+
def print_ok(filename: str) -> None:
25+
print(f"{filename}: OK")
26+
27+
28+
def print_error(filename: str, exc: Exception) -> None:
29+
print(f"{filename}: Error: {exc}")
30+
31+
2432
def print_validationerror(
25-
exc: ValidationError, errors: str = "best-match"
33+
filename: str, exc: ValidationError, errors: str = "best-match"
2634
) -> None:
27-
print("# Validation Error\n")
28-
print(exc)
35+
print(f"{filename}: Validation Error: {exc}")
2936
if exc.cause:
3037
print("\n# Cause\n")
3138
print(exc.cause)
@@ -46,7 +53,11 @@ def print_validationerror(
4653

4754
def main(args: Optional[Sequence[str]] = None) -> None:
4855
parser = ArgumentParser()
49-
parser.add_argument("filename", help="Absolute or relative path to file")
56+
parser.add_argument(
57+
"file",
58+
nargs="+",
59+
help="Validate specified file(s).",
60+
)
5061
parser.add_argument(
5162
"--errors",
5263
choices=("best-match", "all"),
@@ -63,38 +74,40 @@ def main(args: Optional[Sequence[str]] = None) -> None:
6374
)
6475
args_parsed = parser.parse_args(args)
6576

66-
# choose source
67-
reader = read_from_filename
68-
if args_parsed.filename in ["-", "/-"]:
69-
reader = read_from_stdin
70-
71-
# read source
72-
try:
73-
spec, spec_url = reader(args_parsed.filename)
74-
except Exception as exc:
75-
print(exc)
76-
sys.exit(1)
77-
78-
# choose the validator
79-
validators = {
80-
"2.0": openapi_v2_spec_validator,
81-
"3.0.0": openapi_v30_spec_validator,
82-
"3.1.0": openapi_v31_spec_validator,
83-
"detect": openapi_spec_validator_proxy,
84-
}
85-
validator = validators[args_parsed.schema]
86-
87-
# validate
88-
try:
89-
validator.validate(spec, spec_url=spec_url)
90-
except ValidationError as exc:
91-
print_validationerror(exc, args_parsed.errors)
92-
sys.exit(1)
93-
except Exception as exc:
94-
print(exc)
95-
sys.exit(2)
96-
else:
97-
print("OK")
77+
for filename in args_parsed.file:
78+
# choose source
79+
reader = read_from_filename
80+
if filename in ["-", "/-"]:
81+
filename = "stdin"
82+
reader = read_from_stdin
83+
84+
# read source
85+
try:
86+
spec, spec_url = reader(filename)
87+
except Exception as exc:
88+
print(exc)
89+
sys.exit(1)
90+
91+
# choose the validator
92+
validators = {
93+
"2.0": openapi_v2_spec_validator,
94+
"3.0.0": openapi_v30_spec_validator,
95+
"3.1.0": openapi_v31_spec_validator,
96+
"detect": openapi_spec_validator_proxy,
97+
}
98+
validator = validators[args_parsed.schema]
99+
100+
# validate
101+
try:
102+
validator.validate(spec, spec_url=spec_url)
103+
except ValidationError as exc:
104+
print_validationerror(filename, exc, args_parsed.errors)
105+
sys.exit(1)
106+
except Exception as exc:
107+
print_error(filename, exc)
108+
sys.exit(2)
109+
else:
110+
print_ok(filename)
98111

99112

100113
if __name__ == "__main__":

0 commit comments

Comments
 (0)