Skip to content

Commit

Permalink
Improve options validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Guts committed Mar 2, 2023
1 parent 1ce71a7 commit 7373c21
Showing 1 changed file with 36 additions and 7 deletions.
43 changes: 36 additions & 7 deletions qgis_deployment_toolbelt/jobs/job_environment_variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,17 +133,46 @@ def prepare_value(self, value: str) -> str:

return str(value).strip()

def validate_options(self, options: List[dict]) -> List[dict]:
# -- INTERNAL LOGIC ------------------------------------------------------
def validate_options(self, options: dict) -> bool:
"""Validate options.
:param List[dict] options: options to validate.
:return List[dict]: options if they are valid.
:param dict options: options to validate.
:return bool: True if options are valid.
"""
if not isinstance(options, list):
raise TypeError(f"Options must be a list, not {type(options)}")
for option in options:
if not isinstance(option, dict):
raise TypeError(f"Options must be a dict, not {type(option)}")
if option not in self.OPTIONS_SCHEMA:
raise Exception(
f"Job: {self.ID}. Option '{option}' is not valid."
f" Valid options are: {self.OPTIONS_SCHEMA.keys()}"
)

option_in = options.get(option)
option_def: dict = self.OPTIONS_SCHEMA.get(option)
# check value type
if not isinstance(option_in, option_def.get("type")):
raise Exception(
f"Job: {self.ID}. Option '{option}' has an invalid value."
f"\nExpected {option_def.get('type')}, got {type(option_in)}"
)
# check value condition
if option_def.get("condition") == "startswith" and not option_in.startswith(
option_def.get("possible_values")
):
raise Exception(
f"Job: {self.ID}. Option '{option}' has an invalid value."
"\nExpected: starts with one of: "
f"{', '.join(option_def.get('possible_values'))}"
)
elif option_def.get(
"condition"
) == "in" and option_in not in option_def.get("possible_values"):
raise Exception(
f"Job: {self.ID}. Option '{option}' has an invalid value."
f"\nExpected: one of: {', '.join(option_def.get('possible_values'))}"
)
else:
pass

return options

Expand Down

0 comments on commit 7373c21

Please sign in to comment.