Skip to content

Commit

Permalink
pricing validator #9
Browse files Browse the repository at this point in the history
  • Loading branch information
enthec-opensource committed Oct 5, 2023
1 parent fb09331 commit 6804c5e
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions .github/workflows/scripts/technology_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ def __init__(self, msg: str):
super().__init__(msg)


class InvalidPriceException(Exception):
def __init__(self, msg: str):
super().__init__(msg)


class AbstractValidator:
def __init__(self, required: bool = False):
self._required = required
Expand Down Expand Up @@ -80,7 +85,17 @@ def get_type(self) -> list[Type]:
return [str]


class ArrayValidator(AbstractValidator):
class PricingValidator(AbstractValidator):
def _validate(self, data: Any) -> bool:
type_validator: bool = super()._validate(data)
if not type_validator:
return False
for price in data:
if price not in ("low", "mid", "high", "freemium", "poa", "payg", "onetime", "recurring"):
self._set_custom_error(InvalidPriceException(f"Pricing '{price}' is not valid"))
return False
return True

def get_type(self) -> list[Type]:
return [list]

Expand All @@ -96,11 +111,14 @@ def __init__(self, contains_regex: bool = False):
self._contains_regex = contains_regex

def _validate(self, data: Any) -> bool:
type_validator: bool = super()._validate(data)
if not type_validator:
return False
if self._contains_regex:
valid: bool = self._validate_regex(data)
if not valid:
return False
return super()._validate(data)
return True

def _validate_regex(self, data: Any) -> bool:
if type(data) == str:
Expand Down Expand Up @@ -196,7 +214,7 @@ def __init__(self, file_name: str):
"cpe": StringValidator(), # TODO cpe regex
"saas": BoolValidator(),
"oss": BoolValidator(),
"pricing": ArrayValidator(),
"pricing": PricingValidator(),
"implies": StringOrArrayValidator(), # TODO cat validation
"requires": StringOrArrayValidator(), # TODO ^
"excludes": StringOrArrayValidator(), # TODO ^
Expand Down Expand Up @@ -265,6 +283,6 @@ def process(self) -> None:


if __name__ == '__main__':
# for letter in string.ascii_lowercase + "_":
# TechnologiesValidator(os.getenv("TECH_FILE_NAME", f"{letter}.json")).validate()
TechnologiesValidator(os.getenv("TECH_FILE_NAME", f"a.json")).validate()
for letter in string.ascii_lowercase + "_":
TechnologiesValidator(os.getenv("TECH_FILE_NAME", f"{letter}.json")).validate()
# TechnologiesValidator(os.getenv("TECH_FILE_NAME", f"a.json")).validate()

0 comments on commit 6804c5e

Please sign in to comment.