Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(new-form-builder): updated the regex for initial fields to not allow empty string #1888

Merged
merged 3 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
176 changes: 176 additions & 0 deletions scripts/validateRegex.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
import os
import json
import re
from collections import defaultdict


def validate_ui_config_file(file_path):
try:
with open(file_path, "r") as f:
data = json.load(f)

# Ensure the root data is always an object
if not isinstance(data, dict):
raise ValueError("Root data should be an object")

# Check if uiConfig is an array
ui_config = data.get("uiConfig", {})
if isinstance(ui_config, list):
return {"status": "old", "regexes": []} # Mark as old config

# If uiConfig is an object, validate the structure
if isinstance(ui_config, dict):
base_template = ui_config.get("baseTemplate", [])
if not isinstance(base_template, list) or not base_template:
raise ValueError("'baseTemplate' is missing or invalid")

sections = base_template[0].get("sections", [])
if not isinstance(sections, list):
raise ValueError("'sections' should be a list")

regexes = []
invalid_fields = []
# Iterate over each section
for section in sections:
if not isinstance(section, dict):
raise ValueError("Section should be a dictionary")

groups = section.get("groups", [])
if not isinstance(groups, list):
raise ValueError("'groups' in a section should be a list")

# Iterate over each group's fields
for group in groups:
if not isinstance(group, dict):
raise ValueError("Group should be a dictionary")

fields = group.get("fields", [])
if not isinstance(fields, list):
raise ValueError("'fields' in a group should be a list")

for field in fields:
if not isinstance(field, dict):
raise ValueError("Field should be a dictionary")

if field.get("type") == "textInput":
regex = field.get("regex")
regex_error_message = field.get("regexErrorMessage")
config_key = field.get("configKey", "unknown")

# Check if regex and regexErrorMessage are defined
if not regex or not regex_error_message:
invalid_fields.append(
{
"configKey": config_key,
"error": "'regex' or 'regexErrorMessage' missing",
}
)

# Check if the regex allows an empty string
elif re.fullmatch(regex, ""):
invalid_fields.append(
{
"configKey": config_key,
"error": "'regex' allows empty string",
}
)

# Collect regex for reporting if valid
else:
regexes.append(regex)

if invalid_fields:
return {"status": "fail", "invalidFields": invalid_fields}

return {"status": "pass", "regexes": regexes} # Validation passed

except Exception as e:
return {"status": "fail", "error": str(e)} # Return error message


def validate_all_configs(base_path):
invalid_directories = defaultdict(list)
valid_directories = []
old_configs = []

if not os.path.exists(base_path):
raise FileNotFoundError(
f"The base path '{base_path}' does not exist. Please provide a valid path."
)

for root, dirs, files in os.walk(base_path):
if "ui-config.json" in files:
file_path = os.path.join(root, "ui-config.json")
validation_result = validate_ui_config_file(file_path)

directory_name = os.path.basename(root)

# Separate passed, failed, and old configurations
if validation_result["status"] == "fail":
if "invalidFields" in validation_result:
for field in validation_result["invalidFields"]:
invalid_directories[field["error"]].append(
{
"directory": directory_name,
"configKey": field["configKey"],
}
)
else:
invalid_directories[validation_result["error"]].append(
{"directory": directory_name}
)
elif validation_result["status"] == "old":
old_configs.append(directory_name)
else:
valid_directories.append(
{
"directory": directory_name,
"regexes": validation_result["regexes"],
}
)

return valid_directories, invalid_directories, old_configs


if __name__ == "__main__":
# Define the base path where the 'destinations' folder is located
base_path = os.path.abspath(
os.path.join(os.path.dirname(__file__), "../src/configurations/destinations")
)

# Validate base path
try:
valid_dirs, invalid_dirs, old_configs = validate_all_configs(base_path)
except FileNotFoundError as e:
print(f"Error: {e}")
exit(1)

# Output results
print("\nValidation Summary:\n")
print(f"Valid Directories: {len(valid_dirs)}")
print(f"Invalid Directories: {sum(len(v) for v in invalid_dirs.values())}")
print(f"Old Configs: {len(old_configs)}\n")

if valid_dirs:
print("Details of Valid Directories:")
for entry in valid_dirs:
print(
f"Destination: {entry['directory']}, Regexes: {', '.join(entry['regexes'])}"
)
print()

if invalid_dirs:
print("Details of Invalid Directories:")
for error, directories in invalid_dirs.items():
print(f"Error: {error}")
for entry in directories:
print(
f" - Destination: {entry['directory']}, ConfigKey: {entry.get('configKey', 'N/A')}"
)
print()

if old_configs:
print("Details of Old Configs:")
for directory in old_configs:
print(f"Destination: {directory}")
print()
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
"apiKey": {
"type": "string",
"pattern": "(^\\{\\{.*\\|\\|(.*)\\}\\}$)|(^env[.].+)|.*"
"pattern": "(^\\{\\{.*\\|\\|(.*)\\}\\}$)|(^env[.].+)|^(.{1,100})$"
},
"actid": {
"type": "string",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,15 @@
"configKey": "apiUrl",
"regex": "(^\\{\\{.*\\|\\|(.*)\\}\\}$)|(^env[.].+)|(?!.*\\.ngrok\\.io)^(?:http(s)?:\\/\\/)?[\\w.-]+(?:\\.[\\w\\.-]+)+[\\w\\-\\._~:/?#[\\]@!\\$&'\\(\\)\\*\\+,;=.]+$",
"regexErrorMessage": "Invalid URL",
"required": true,
"placeholder": "e.g. https://accountname.api-us1.com",
"note": "Your API Access URL"
},
{
"type": "textInput",
"label": "API Key",
"configKey": "apiKey",
"regex": ".*",
"required": true,
"regex": "^(.{1,100})$",
"regexErrorMessage": "Invalid API Key",
"placeholder": "e.g. c5Xe3398cla9fXXXXf95094eXa0c1e8b32X451bbpm8j2",
"secret": true,
"footerNote": "Your API Access Key"
Expand Down
2 changes: 1 addition & 1 deletion src/configurations/destinations/bloomreach/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"properties": {
"apiBaseUrl": {
"type": "string",
"pattern": "(^\\{\\{.*\\|\\|(.*)\\}\\}$)|(^env[.].+)|(?!.*\\.ngrok\\.io)^(?:http(s)?:\\/\\/)?[\\w.-]+(?:\\.[\\w\\.-]+)+[\\w\\-\\._~:/?#[\\]@!\\$&'\\(\\)\\*\\+,;=.]+$|^$"
"pattern": "(^\\{\\{.*\\|\\|(.*)\\}\\}$)|(^env[.].+)|(?!.*\\.ngrok\\.io)^(?:http(s)?:\\/\\/)?[\\w.-]+(?:\\.[\\w\\.-]+)+[\\w\\-\\._~:/?#[\\]@!\\$&'\\(\\)\\*\\+,;=.]{1,500}$"
},
"apiKey": {
"type": "string",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"label": "API Base URL",
"note": "URL of the Bloomreach instance",
"configKey": "apiBaseUrl",
"regex": "(^\\{\\{.*\\|\\|(.*)\\}\\}$)|(^env[.].+)|(?!.*\\.ngrok\\.io)^(?:http(s)?:\\/\\/)?[\\w.-]+(?:\\.[\\w\\.-]+)+[\\w\\-\\._~:/?#[\\]@!\\$&'\\(\\)\\*\\+,;=.]+$|^$",
"regex": "(^\\{\\{.*\\|\\|(.*)\\}\\}$)|(^env[.].+)|(?!.*\\.ngrok\\.io)^(?:http(s)?:\\/\\/)?[\\w.-]+(?:\\.[\\w\\.-]+)+[\\w\\-\\._~:/?#[\\]@!\\$&'\\(\\)\\*\\+,;=.]{1,500}$",
"regexErrorMessage": "Invalid API Base URl",
"placeholder": "e.g. https://demoapp-api.bloomreach.com"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"properties": {
"apiBaseUrl": {
"type": "string",
"pattern": "(^\\{\\{.*\\|\\|(.*)\\}\\}$)|(^env[.].+)|(?!.*\\.ngrok\\.io)^(?:http(s)?:\\/\\/)?[\\w.-]+(?:\\.[\\w\\.-]+)+[\\w\\-\\._~:/?#[\\]@!\\$&'\\(\\)\\*\\+,;=.]+$|^$"
"pattern": "(^\\{\\{.*\\|\\|(.*)\\}\\}$)|(^env[.].+)|(?!.*\\.ngrok\\.io)^(?:http(s)?:\\/\\/)?[\\w.-]+(?:\\.[\\w\\.-]+)+[\\w\\-\\._~:/?#[\\]@!\\$&'\\(\\)\\*\\+,;=.]{1,500}$"
},
"apiKey": {
"type": "string",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"label": "API Base URL",
"note": "URL of the Bloomreach instance",
"configKey": "apiBaseUrl",
"regex": "(^\\{\\{.*\\|\\|(.*)\\}\\}$)|(^env[.].+)|(?!.*\\.ngrok\\.io)^(?:http(s)?:\\/\\/)?[\\w.-]+(?:\\.[\\w\\.-]+)+[\\w\\-\\._~:/?#[\\]@!\\$&'\\(\\)\\*\\+,;=.]+$|^$",
"regex": "(^\\{\\{.*\\|\\|(.*)\\}\\}$)|(^env[.].+)|(?!.*\\.ngrok\\.io)^(?:http(s)?:\\/\\/)?[\\w.-]+(?:\\.[\\w\\.-]+)+[\\w\\-\\._~:/?#[\\]@!\\$&'\\(\\)\\*\\+,;=.]{1,500}$",
"regexErrorMessage": "Invalid API Base URl",
"placeholder": "e.g. https://demoapp-api.bloomreach.com"
},
Expand Down
2 changes: 1 addition & 1 deletion src/configurations/destinations/cordial/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
"apiBaseUrl": {
"type": "string",
"pattern": "(^\\{\\{.*\\|\\|(.*)\\}\\}$)|(^env[.].+)|(?!.*\\.ngrok\\.io)^(?:http(s)?:\\/\\/)?[\\w.-]+(?:\\.[\\w\\.-]+)+[\\w\\-\\._~:/?#[\\]@!\\$&'\\(\\)\\*\\+,;=.]+$|^$"
"pattern": "(^\\{\\{.*\\|\\|(.*)\\}\\}$)|(^env[.].+)|(?!.*\\.ngrok\\.io)^(?:http(s)?:\\/\\/)?[\\w.-]+(?:\\.[\\w\\.-]+)+[\\w\\-\\._~:/?#[\\]@!\\$&'\\(\\)\\*\\+,;=.]{1,500}$"
},
"oneTrustCookieCategories": {
"type": "object",
Expand Down
2 changes: 1 addition & 1 deletion src/configurations/destinations/cordial/ui-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"label": "API Base URL",
"note": "You can find out which one your account uses by navigating to your Cordial dashboard and checking the website URL.",
"configKey": "apiBaseUrl",
"regex": "(^\\{\\{.*\\|\\|(.*)\\}\\}$)|(^env[.].+)|(?!.*\\.ngrok\\.io)^(?:http(s)?:\\/\\/)?[\\w.-]+(?:\\.[\\w\\.-]+)+[\\w\\-\\._~:/?#[\\]@!\\$&'\\(\\)\\*\\+,;=.]+$|^$",
"regex": "(^\\{\\{.*\\|\\|(.*)\\}\\}$)|(^env[.].+)|(?!.*\\.ngrok\\.io)^(?:http(s)?:\\/\\/)?[\\w.-]+(?:\\.[\\w\\.-]+)+[\\w\\-\\._~:/?#[\\]@!\\$&'\\(\\)\\*\\+,;=.]{1,500}$",
"regexErrorMessage": "Invalid API Base URl",
"placeholder": "e.g. https://usw2.admin.cordial.io"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
"accessToken": {
"type": "string",
"pattern": "(^\\{\\{.*\\|\\|(.*)\\}\\}$)|(^env[.].+)|^(.{0,500})$"
"pattern": "(^\\{\\{.*\\|\\|(.*)\\}\\}$)|(^env[.].+)|^(.{1,500})$"
},
"eventsToEvents": {
"type": "array",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"label": "Business Access Token",
"note": "Your Business Access token from your Business Account. Required for cloud-mode.",
"configKey": "accessToken",
"regex": "(^\\{\\{.*\\|\\|(.*)\\}\\}$)|(^env[.].+)|^(.{0,500})$",
"regex": "(^\\{\\{.*\\|\\|(.*)\\}\\}$)|(^env[.].+)|^(.{1,500})$",
"regexErrorMessage": "Invalid Business Access Token",
"placeholder": "e.g: EAALPFdyOVl4BAKEXmVR...",
"secret": true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@
"properties": {
"audienceId": {
"type": "string",
"pattern": "(^\\{\\{.*\\|\\|(.*)\\}\\}$)|(^env[.].+)|^(.{0,100})$"
"pattern": "(^\\{\\{.*\\|\\|(.*)\\}\\}$)|(^env[.].+)|^(.{1,100})$"
}
},
"required": ["audienceId"]
Expand All @@ -412,7 +412,7 @@
"properties": {
"adAccountId": {
"type": "string",
"pattern": "(^\\{\\{.*\\|\\|(.*)\\}\\}$)|(^env[.].+)|^(.{0,100})$"
"pattern": "(^\\{\\{.*\\|\\|(.*)\\}\\}$)|(^env[.].+)|^(.{1,100})$"
}
},
"required": ["adAccountId"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"label": "Ad Account id",
"note": "Enter the Ad Account id of your business application set up",
"configKey": "adAccountId",
"regex": "(^\\{\\{.*\\|\\|(.*)\\}\\}$)|(^env[.].+)|^(.{0,100})$",
"regex": "(^\\{\\{.*\\|\\|(.*)\\}\\}$)|(^env[.].+)|^(.{1,100})$",
"regexErrorMessage": "Invalid Ad Account id",
"placeholder": "e.g: 2309XXX5483",
"preRequisites": {
Expand All @@ -44,7 +44,8 @@
"label": "Audience Id",
"note": "Enter your audience id",
"configKey": "audienceId",
"regex": "(^\\{\\{.*\\|\\|(.*)\\}\\}$)|(^env[.].+)|^(.{0,100})$",
"regex": "(^\\{\\{.*\\|\\|(.*)\\}\\}$)|(^env[.].+)|^(.{1,100})$",
"regexErrorMessage": "Invalid Audience id",
"placeholder": "e.g: 238476XXXX5910030",
"preRequisites": {
"fields": [
Expand Down
Loading
Loading