AW Dev Rethought

🕵️ Debugging is like being the detective in a crime movie where you are also the murderer - Filipe Fortes

🧩 Python Automation Recipes – ⚙️ Config Validator


Description:

📌 Introduction

Automation workflows often depend on configuration files. But if a config is missing required fields or has invalid values, the automation may fail halfway through execution.

This recipe shows how to validate JSON/YAML configs before execution, so your scripts can catch issues early and fail safely.


🔎 Explanation

  • The script loads a config file.
  • It checks whether all required fields exist.
  • It validates basic value types like strings, lists, and booleans.
  • If validation fails, it prints clear errors.
  • If validation passes, the automation can safely continue.

This is useful before running workflow runners, scheduled jobs, backup scripts, or deployment automations.


✅ Key Takeaways

  • ⚙️ Validate configs before running automation tasks.
  • 🚨 Catch missing or invalid fields early.
  • 🧩 Makes automation workflows safer and more reliable.

📄 Sample config.json

{
  "workflow_name": "daily_automation",
  "enabled": true,
  "tasks": [
    "backup_folder",
    "clean_logs",
    "send_report"
  ]
}

Code Snippet:

import json
from pathlib import Path

# --- Step 1: Configure config file path ---
CONFIG_FILE = Path("config.json")

# --- Step 2: Define required config schema ---
REQUIRED_FIELDS = {
    "workflow_name": str,
    "enabled": bool,
    "tasks": list,
}

# --- Step 3: Load JSON config ---
def load_config(file_path):
    """
    Load and return JSON config data.
    """
    if not file_path.exists():
        raise FileNotFoundError(f"Config file not found: {file_path}")

    with file_path.open("r", encoding="utf-8") as file:
        return json.load(file)

# --- Step 4: Validate config structure ---
def validate_config(config):
    """
    Validate required fields and expected data types.
    """
    errors = []

    for field, expected_type in REQUIRED_FIELDS.items():

        # Check if field exists
        if field not in config:
            errors.append(f"Missing required field: {field}")
            continue

        # Check if field has correct type
        if not isinstance(config[field], expected_type):
            errors.append(
                f"Invalid type for '{field}'. "
                f"Expected {expected_type.__name__}, got {type(config[field]).__name__}"
            )

    # Check that tasks list is not empty
    if "tasks" in config and isinstance(config["tasks"], list):
        if not config["tasks"]:
            errors.append("Tasks list cannot be empty.")

        # Check every task is a string
        for task in config["tasks"]:
            if not isinstance(task, str):
                errors.append("Every task inside 'tasks' must be a string.")

    return errors

# --- Step 5: Run validation ---
try:
    config_data = load_config(CONFIG_FILE)
    validation_errors = validate_config(config_data)

    if validation_errors:
        print("Config validation failed:\n")

        for error in validation_errors:
            print(f"- {error}")

    else:
        print("Config validation passed.")
        print(f"Workflow ready: {config_data['workflow_name']}")

except json.JSONDecodeError as e:
    print("Invalid JSON format.")
    print(f"Error: {e}")

except Exception as e:
    print("Validation failed.")
    print(f"Error: {e}")

Link copied!

Comments

Add Your Comment

Comment Added!