Skip to content

Allow validate multiple files #255

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 55 additions & 38 deletions openapi_spec_validator/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,18 @@
)


def print_ok(filename: str) -> None:
print(f"{filename}: OK")


def print_error(filename: str, exc: Exception) -> None:
print(f"{filename}: Error: {exc}")


def print_validationerror(
exc: ValidationError, errors: str = "best-match"
filename: str, exc: ValidationError, errors: str = "best-match"
) -> None:
print("# Validation Error\n")
print(exc)
print(f"{filename}: Validation Error: {exc}")
if exc.cause:
print("\n# Cause\n")
print(exc.cause)
Expand All @@ -46,7 +53,11 @@ def print_validationerror(

def main(args: Optional[Sequence[str]] = None) -> None:
parser = ArgumentParser()
parser.add_argument("filename", help="Absolute or relative path to file")
parser.add_argument(
"file",
nargs="+",
help="Validate specified file(s).",
)
parser.add_argument(
"--errors",
choices=("best-match", "all"),
Expand All @@ -56,45 +67,51 @@ def main(args: Optional[Sequence[str]] = None) -> None:
)
parser.add_argument(
"--schema",
help="OpenAPI schema (default: detect)",
type=str,
choices=["2.0", "3.0.0", "3.1.0", "detect"],
choices=["detect", "2.0", "3.0", "3.1", "3.0.0", "3.1.0"],
default="detect",
metavar="{detect,2.0,3.0,3.1}",
help="OpenAPI schema version (default: detect).",
)
args_parsed = parser.parse_args(args)

# choose source
reader = read_from_filename
if args_parsed.filename in ["-", "/-"]:
reader = read_from_stdin

# read source
try:
spec, spec_url = reader(args_parsed.filename)
except Exception as exc:
print(exc)
sys.exit(1)

# choose the validator
validators = {
"2.0": openapi_v2_spec_validator,
"3.0.0": openapi_v30_spec_validator,
"3.1.0": openapi_v31_spec_validator,
"detect": openapi_spec_validator_proxy,
}
validator = validators[args_parsed.schema]

# validate
try:
validator.validate(spec, spec_url=spec_url)
except ValidationError as exc:
print_validationerror(exc, args_parsed.errors)
sys.exit(1)
except Exception as exc:
print(exc)
sys.exit(2)
else:
print("OK")
for filename in args_parsed.file:
# choose source
reader = read_from_filename
if filename in ["-", "/-"]:
filename = "stdin"
reader = read_from_stdin

# read source
try:
spec, spec_url = reader(filename)
except Exception as exc:
print(exc)
sys.exit(1)

# choose the validator
validators = {
"detect": openapi_spec_validator_proxy,
"2.0": openapi_v2_spec_validator,
"3.0": openapi_v30_spec_validator,
"3.1": openapi_v31_spec_validator,
# backward compatibility
"3.0.0": openapi_v30_spec_validator,
"3.1.0": openapi_v31_spec_validator,
}
validator = validators[args_parsed.schema]

# validate
try:
validator.validate(spec, spec_url=spec_url)
except ValidationError as exc:
print_validationerror(filename, exc, args_parsed.errors)
sys.exit(1)
except Exception as exc:
print_error(filename, exc)
sys.exit(2)
else:
print_ok(filename)


if __name__ == "__main__":
Expand Down