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 #1881

Closed
wants to merge 1 commit into from
Closed
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\\-\\._~:/?#[\\]@!\\$&'\\(\\)\\*\\+,;=.]+$"
},
"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,100})$",
"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\\-\\._~:/?#[\\]@!\\$&'\\(\\)\\*\\+,;=.]+$"
},
"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,100})$",
"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\\-\\._~:/?#[\\]@!\\$&'\\(\\)\\*\\+,;=.]+$"
},
"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,100})$",
"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})$"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

Schema validation is not enforcing the minimum length requirement

The schema correctly defines the pattern ^(.{1,100})$ requiring at least one character, but validation tests reveal that empty strings are still being accepted for both audienceId and adAccountId. This indicates a potential issue with the schema validation implementation.

🔗 Analysis chain

LGTM! Schema validation has been strengthened.

The regex patterns for audienceId and adAccountId have been updated to require at least one character while maintaining support for template variables and environment variables.

Run the following script to verify the schema validation:

Also applies to: 415-415

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that the schema validation correctly enforces non-empty strings
# Test both valid and invalid cases for audienceId and adAccountId

echo '{"connectionMode":{"cloud":"cloud"},"audienceId":""}' | python3 -c '
import sys, json
from jsonschema import validate
schema = json.load(open("src/configurations/destinations/fb_custom_audience/schema.json"))
try:
    validate(json.loads(sys.stdin.read()), schema)
    print("Validation should have failed for empty audienceId")
    exit(1)
except Exception as e:
    print("Validation failed as expected:", str(e))
'

echo '{"connectionMode":{"warehouse":"cloud"},"adAccountId":""}' | python3 -c '
import sys, json
from jsonschema import validate
schema = json.load(open("src/configurations/destinations/fb_custom_audience/schema.json"))
try:
    validate(json.loads(sys.stdin.read()), schema)
    print("Validation should have failed for empty adAccountId")
    exit(1)
except Exception as e:
    print("Validation failed as expected:", str(e))
'

Length of output: 960


Script:

#!/bin/bash
# Inspect the schema structure for audienceId and adAccountId
cat src/configurations/destinations/fb_custom_audience/schema.json | jq '.properties | {audienceId: .audienceId, adAccountId: .adAccountId}'

Length of output: 190


Script:

#!/bin/bash
# View the complete schema structure to locate audienceId and adAccountId
cat src/configurations/destinations/fb_custom_audience/schema.json | jq '.'

Length of output: 12784


Script:

#!/bin/bash
# Test with correct schema path and proper structure
echo '{"configSchema":{"connectionMode":{"cloud":"cloud"},"audienceId":""}}' | python3 -c '
import sys, json
from jsonschema import validate
schema = json.load(open("src/configurations/destinations/fb_custom_audience/schema.json"))
try:
    validate(json.loads(sys.stdin.read()), schema)
    print("Validation should have failed for empty audienceId")
    exit(1)
except Exception as e:
    print("Validation failed as expected:", str(e))
'

echo '{"configSchema":{"connectionMode":{"warehouse":"cloud"},"adAccountId":""}}' | python3 -c '
import sys, json
from jsonschema import validate
schema = json.load(open("src/configurations/destinations/fb_custom_audience/schema.json"))
try:
    validate(json.loads(sys.stdin.read()), schema)
    print("Validation should have failed for empty adAccountId")
    exit(1)
except Exception as e:
    print("Validation failed as expected:", str(e))
'

Length of output: 994

}
},
"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
4 changes: 2 additions & 2 deletions src/configurations/destinations/fullstory/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,7 @@
"properties": {
"apiKey": {
"type": "string",
"pattern": "(^\\{\\{.*\\|\\|(.*)\\}\\}$)|(^env[.].+)|^(.{0,200})$"
"pattern": "(^\\{\\{.*\\|\\|(.*)\\}\\}$)|(^env[.].+)|^(.{1,200})$"
}
},
"required": ["apiKey"]
Expand All @@ -976,7 +976,7 @@
"properties": {
"fs_org": {
"type": "string",
"pattern": "(^\\{\\{.*\\|\\|(.*)\\}\\}$)|(^env[.].+)|^(.{0,100})$"
"pattern": "(^\\{\\{.*\\|\\|(.*)\\}\\}$)|(^env[.].+)|^(.{1,100})$"
}
},
"required": ["fs_org"]
Expand Down
4 changes: 2 additions & 2 deletions src/configurations/destinations/fullstory/ui-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"label": "API Key",
"note": "The API key must have Admin or Architect level permissions to view or delete data. Required for cloud mode.",
"configKey": "apiKey",
"regex": "^(.{0,200})$",
"regex": "^(.{1,200})$",
"regexErrorMessage": "Invalid Api Key",
"placeholder": "e.g: na1.XQdLJo0...",
"secret": true,
Expand Down Expand Up @@ -76,7 +76,7 @@
"label": "FS ORG",
"note": "Required for web device mode,",
"configKey": "fs_org",
"regex": "^(.{0,100})$",
"regex": "^(.{1,100})$",
"regexErrorMessage": "Invalid FS ORG",
"placeholder": "e.g: ddXXX940",
"preRequisites": {
Expand Down
4 changes: 2 additions & 2 deletions src/configurations/destinations/ga4/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"properties": {
"apiSecret": {
"type": "string",
"pattern": "(^\\{\\{.*\\|\\|(.*)\\}\\}$)|(^env[.].+)|^(.{0,100})$"
"pattern": "(^\\{\\{.*\\|\\|(.*)\\}\\}$)|(^env[.].+)|^(.{1,100})$"
},
"typesOfClient": {
"type": "string",
Expand Down Expand Up @@ -955,7 +955,7 @@
"properties": {
"measurementId": {
"type": "string",
"pattern": "(^\\{\\{.*\\|\\|(.*)\\}\\}$)|(^env[.].+)|^(G-.{1,100})$|^$"
"pattern": "(^\\{\\{.*\\|\\|(.*)\\}\\}$)|(^env[.].+)|^(G-.{1,100})$"
}
},
"required": ["measurementId"],
Expand Down
4 changes: 2 additions & 2 deletions src/configurations/destinations/ga4/ui-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"label": "API Secret",
"note": "Enter the API secret associated with your GA4 data stream. Create this in your GA UI under Admin > Data Streams > choose your stream > Measurement Protocol API Secrets > Create",
"configKey": "apiSecret",
"regex": "(^\\{\\{.*\\|\\|(.*)\\}\\}$)|(^env[.].+)|^(.{0,100})$",
"regex": "(^\\{\\{.*\\|\\|(.*)\\}\\}$)|(^env[.].+)|^(.{1,100})$",
"regexErrorMessage": "Invalid API Secret",
"placeholder": "e.g: QuIUHRj7XXXXX7eZDZiPQU",
"secret": true
Expand Down Expand Up @@ -50,7 +50,7 @@
},
"label": "Measurement Id",
"configKey": "measurementId",
"regex": "(^\\{\\{.*\\|\\|(.*)\\}\\}$)|(^env[.].+)|^(G-.{1,100})$|^$",
"regex": "(^\\{\\{.*\\|\\|(.*)\\}\\}$)|(^env[.].+)|^(G-.{1,100})$|^(.{1,100})$",
"regexErrorMessage": "Invalid Measurement Id",
"note": "Enter the ID associated with your stream. Find this under Admin > Data Streams > choose your stream > Measurement ID",
"placeholder": "e.g: G-AB1CD2E34F",
Expand Down
Loading
Loading