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+
2432def 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
4754def 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" ),
@@ -56,45 +67,51 @@ def main(args: Optional[Sequence[str]] = None) -> None:
5667 )
5768 parser .add_argument (
5869 "--schema" ,
59- help = "OpenAPI schema (default: detect)" ,
6070 type = str ,
61- choices = ["2.0" , "3.0.0 " , "3.1.0 " , "detect " ],
71+ choices = ["detect" , " 2.0" , "3.0" , "3.1" , "3.0.0 " , "3.1.0 " ],
6272 default = "detect" ,
73+ metavar = "{detect,2.0,3.0,3.1}" ,
74+ help = "OpenAPI schema version (default: detect)." ,
6375 )
6476 args_parsed = parser .parse_args (args )
6577
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" )
78+ for filename in args_parsed .file :
79+ # choose source
80+ reader = read_from_filename
81+ if filename in ["-" , "/-" ]:
82+ filename = "stdin"
83+ reader = read_from_stdin
84+
85+ # read source
86+ try :
87+ spec , spec_url = reader (filename )
88+ except Exception as exc :
89+ print (exc )
90+ sys .exit (1 )
91+
92+ # choose the validator
93+ validators = {
94+ "detect" : openapi_spec_validator_proxy ,
95+ "2.0" : openapi_v2_spec_validator ,
96+ "3.0" : openapi_v30_spec_validator ,
97+ "3.1" : openapi_v31_spec_validator ,
98+ # backward compatibility
99+ "3.0.0" : openapi_v30_spec_validator ,
100+ "3.1.0" : openapi_v31_spec_validator ,
101+ }
102+ validator = validators [args_parsed .schema ]
103+
104+ # validate
105+ try :
106+ validator .validate (spec , spec_url = spec_url )
107+ except ValidationError as exc :
108+ print_validationerror (filename , exc , args_parsed .errors )
109+ sys .exit (1 )
110+ except Exception as exc :
111+ print_error (filename , exc )
112+ sys .exit (2 )
113+ else :
114+ print_ok (filename )
98115
99116
100117if __name__ == "__main__" :
0 commit comments