vinod's erudition

we learn everything from failure not from success so keep failing

Home Ruby Rails Javascript Python Agentic AI

Configuration File Validator

Posted on April 19, 2025 by vinod

Configuration File Validator in Python

When building or deploying software, configuration files are often used to control behavior without modifying the source code. Validating these configuration files ensures they meet expected structures and types. This blog explores a Python script that validates configuration files in JSON or YAML format against a defined schema.


Features


Python Script

import json
import yaml


class Config_Schema_Validator:

    def __init__(self):
        self.config_data = None

    def load_data(self, config):
        if config.endswith(".json"):
            with open(config, 'r') as file:
                self.config_data = json.load(file)
        elif config.endswith(".yaml"):
            with open(config, 'r') as file:
                self.config_data = yaml.safe_load(file)
        else:
            raise ValueError("invalid file format")
        return self.config_data

    def validate(self, config, schema, path=""):
        errors = []
        for key, value in schema.items():
            full_key = f"{path}.{key}" if path else key
            if key not in config:
                errors.append(f"Missing key: {full_key}")
            else:
                actual_value = config[key]
                if isinstance(value, dict):
                    if isinstance(actual_value, dict):
                        errors.extend(self.validate(actual_value, value, full_key))
                    else:
                        errors.append(
                            f"Type Mismath: {full_key} - Expected {type(value).__name__}, got {type(actual_value).__name__}"
                        )
                else:
                    if not isinstance(actual_value, value):
                        errors.append(
                            f"Type Mismath: {full_key} - Expected {type(value).__name__}, got {type(actual_value).__name__}"
                        )
        return errors


if __name__ == "__main__":
    schema = {
        "app_name": str,
        "version": str,
        "debug": bool,
        "database": {
            "host": str,
            "port": int
        }
    }

    csv = Config_Schema_Validator()
    config_data = csv.load_data('config.json')
    print(config_data)
    validation_errors = csv.validate(config_data, schema)

    if validation_errors:
        print("Validation Errors:")
        for err in validation_errors:
            print(" -", err)
    else:
        print("Config is valid as per schema")


{ “app_name”: “myapp”, “version”: “1.0.0”, “debug”: true, “database”: { “host”: 12, “port”: 5432 } }

## Output

{‘app_name’: ‘MyApp’, ‘version’: ‘1.0’, ‘debug’: True, ‘database’: {‘host’: 12, ‘port’: ‘5432’}} Validation Errprs:

Learnt

Happy Coding !!