@@ -181,26 +181,28 @@ def validate_input_path(ctx, param, value):
181
181
Validate a ``value`` list of inputs path strings
182
182
"""
183
183
options = ctx .params
184
- from_json = options .get ("--from-json " , False )
184
+ from_json = options .get ("from_json " , False )
185
185
for inp in value :
186
186
if not (is_file (location = inp , follow_symlinks = True ) or is_dir (location = inp , follow_symlinks = True )):
187
187
raise click .BadParameter (f"input: { inp !r} is not a regular file or a directory" )
188
188
189
189
if not is_readable (location = inp ):
190
190
raise click .BadParameter (f"input: { inp !r} is not readable" )
191
191
192
- if from_json and not is_file (location = inp , follow_symlinks = True ):
193
- # extra JSON validation
194
- raise click .BadParameter (f"JSON input: { inp !r} is not a file" )
192
+ if from_json :
193
+ if is_dir (location = inp , follow_symlinks = True ):
194
+ raise click .BadParameter ("Error: Invalid value: Input JSON scan file(s) is not valid JSON" )
195
+
195
196
if not inp .lower ().endswith (".json" ):
196
- raise click .BadParameter (f"JSON input: { inp !r} is not a JSON file with a .json extension" )
197
- with open (inp ) as js :
198
- start = js .read (100 ).strip ()
199
- if not start .startswith ("{" ):
200
- raise click .BadParameter (f"JSON input: { inp !r} is not a well formed JSON file" )
197
+ raise click .BadParameter ("Error: Invalid value: Input JSON scan file(s) is not valid JSON" )
201
198
202
- return value
199
+ try :
200
+ with open (inp , 'r' , encoding = 'utf-8' ) as f :
201
+ json .load (f ) # Try to parse the file as JSON
202
+ except (json .JSONDecodeError , UnicodeDecodeError ):
203
+ raise click .BadParameter ("Error: Invalid value: Input JSON scan file(s) is not valid JSON" )
203
204
205
+ return value
204
206
205
207
@click .command (name = 'scancode' ,
206
208
epilog = epilog_text ,
0 commit comments