Skip to content

Commit

Permalink
Update title validator and move package version to init
Browse files Browse the repository at this point in the history
  • Loading branch information
cmcnally-r7 committed Mar 10, 2024
1 parent cc88caf commit fadf9ef
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 51 deletions.
1 change: 1 addition & 0 deletions icon_validator/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
VERSION = "2.47.15"
148 changes: 99 additions & 49 deletions icon_validator/rules/plugin_validators/title_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,89 +4,139 @@


class TitleValidator(KomandPluginValidator):
def __init__(self):
super().__init__()

self.all_offenses: str = ""

def validate_title(self, title: str, plugin_title: bool = False) -> None:
"""
:param title:
:param plugin_title:
"""

@staticmethod
def validate_title(title, plugin_title=False):
title_validation_list_lowercase = '", "'.join(title_validation_list).lower()

if not isinstance(title, str):
raise ValidationException("Title must not be blank")
self.all_offenses += (
f"Title must be a string, not a number or other value: {title}\n"
)
if title == "":
raise ValidationException("Title must not be blank")
self.all_offenses += f"Title must not be blank: {title}\n"
if title.endswith("."):
raise ValidationException("Title ends with period when it should not.")
self.all_offenses += f"Title ends with period when it should not: {title}\n"
if title[0].islower() and not plugin_title:
# This plugin title is OK: minFraud
# This plugin title is OK: ifconfig.co
raise ValidationException("Title should not start with a lower case letter.")
self.all_offenses += (
f"Title should not start with a lower case letter: {title}\n"
)
if title[0].isspace():
raise ValidationException("Title should not start with a whitespace character.")
self.all_offenses += (
f"Title should not start with a whitespace character: {title}\n"
)
if len(title.split()) > 7:
raise ValidationException(f"Title is too long, 6 words or less: contains {str(len(title.split()))}")
self.all_offenses += f"Title is too long, 6 words or less: contains {str(len(title.split()))}: {title}\n"
for word in title.split():
if not title.startswith(word):
if word in title_validation_list:
if not title.endswith(word):
raise ValidationException(
'English articles and conjunctions should be lowercase when in the middle of the sentence:'
f' "{title_validation_list_lowercase}"'
)
self.all_offenses += f"English articles and conjunctions should be lowercase when in the middle of the sentence:'{title_validation_list_lowercase}': {title}\n"

elif "By" == word and not title.endswith("By"):
# This is OK: Order By
# This is NOT OK: Search By String
raise ValidationException("Title contains a capitalized 'By' when it should not.")
self.all_offenses += f"Title contains a capitalized 'By' when it should not: {title}\n"
elif "Of" == word and not title.endswith("Of"):
# This is OK: Member Of
# This is NOT OK: Type Of String
raise ValidationException("Title contains a capitalized 'Of' when it should not.")
elif not word[0].isupper() and not word.capitalize() in title_validation_list:
self.all_offenses += f"Title contains a capitalized 'Of' when it should not: {title}\n"
elif (
not word[0].isupper()
and not word.capitalize() in title_validation_list
):
if not word.lower() == "by" or word.lower() == "of":
if word.isalpha():
raise ValidationException(f"Title contains a lowercase '{word}' when it should not.")
self.all_offenses += f"Title contains a lowercase '{word}' when it should not: {title}\n"

def validate_actions(self, spec: dict, spec_section: str):
"""
@staticmethod
def validate_actions(dict_, dict_key):
if dict_key in dict_:
TitleValidator.validate_dictionary(dict_, dict_key)
for key, value in dict_[dict_key].items():
:param spec:
:param spec_section:
"""

if spec_section in spec:
self.validate_dictionary(spec, spec_section)
for key, value in spec[spec_section].items():
if "input" in value:
TitleValidator.validate_dictionary(value, "input")
self.all_offenses += self.validate_dictionary(
value, "input"
)
if "output" in value:
TitleValidator.validate_dictionary(value, "output")
self.all_offenses += self.validate_dictionary(
value, "output"
)
if "state" in value:
TitleValidator.validate_dictionary(value, "state")
self.all_offenses += self.validate_dictionary(
value, "state"
)
if "schedule" in value:
TitleValidator.validate_dictionary({"schedule": value}, "schedule")
self.all_offenses += self.validate_dictionary(
{"schedule": value}, "schedule"
)

def validate_dictionary(self, spec: dict, section: str) -> None:
"""
:param spec:
:param section:
"""

@staticmethod
def validate_dictionary(dict_, dict_key):
if dict_key in dict_:
if not dict_[dict_key]:
if section in spec:
if not spec[section]:
return

for key, value in dict_[dict_key].items():
if "name" in value:
raise ValidationException(
f"Deprecated 'name' key '{value}' found when 'title' should be used instead.")
for key, value in spec[section].items():
if "title" in value:
try:
TitleValidator.validate_title(value["title"], plugin_title=False)
except Exception as e:
raise ValidationException(f"{dict_key} key '{key}' error.", e)
self.all_offenses += self.validate_title(
value["title"], plugin_title=False
)

@staticmethod
def validate_plugin_title(spec):
if "title" not in spec.spec_dictionary():
raise ValidationException("Plugin title is missing.")
if "name" in value:
self.all_offenses += f"Deprecated 'name' key '{value}' found when 'title' should be used instead: {value}\n"

def validate_plugin_title(self, spec) -> None:
"""
:param spec:
"""

if "title" not in spec.spec_dictionary():
self.all_offenses += f"Plugin title is missing"
try:
TitleValidator.validate_title(spec.spec_dictionary()["title"], plugin_title=True)
except Exception as e:
raise ValidationException("Plugin title not valid.", e)
self.validate_title(
spec.spec_dictionary()["title"], plugin_title=True
)
except Exception as error:
self.all_offenses += f"Plugin title not valid: {error}"

def validate(self, spec):
TitleValidator.validate_plugin_title(spec)
TitleValidator.validate_actions(spec.spec_dictionary(), "actions")
TitleValidator.validate_actions(spec.spec_dictionary(), "triggers")
TitleValidator.validate_actions(spec.spec_dictionary(), "connection")
TitleValidator.validate_actions(spec.spec_dictionary(), "tasks")
"""
:param spec:
"""

self.validate_plugin_title(spec)
self.validate_actions(spec.spec_dictionary(), "actions")
self.validate_actions(spec.spec_dictionary(), "triggers")
self.validate_actions(spec.spec_dictionary(), "tasks")
self.validate_actions(spec.spec_dictionary(), "connection")
if self.all_offenses:
raise ValidationException(self.all_offenses)
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#!/usr/bin/env python

from icon_validator import VERSION
from setuptools import setup, find_packages

with open("README.md", "r") as fh:
long_description = fh.read()

setup(
name="insightconnect_integrations_validators",
version="2.47.14",
version=VERSION,
description="Validator tooling for InsightConnect integrations",
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down

0 comments on commit fadf9ef

Please sign in to comment.