Validate a config file against a schema.
(schema_file, config_file, verbose=False)
| 38 | |
| 39 | |
| 40 | def validate_config(schema_file, config_file, verbose=False): |
| 41 | """Validate a config file against a schema.""" |
| 42 | |
| 43 | # Load schema |
| 44 | try: |
| 45 | with open(schema_file, 'r', encoding='utf-8') as f: |
| 46 | schema = json.load(f) |
| 47 | if verbose: |
| 48 | logging.info(f"Loaded schema from '{schema_file}'") |
| 49 | except Exception as e: |
| 50 | raise RuntimeError(f"Failed to load schema: {e}") |
| 51 | |
| 52 | # Validate schema itself |
| 53 | try: |
| 54 | Draft7Validator.check_schema(schema) |
| 55 | except SchemaError as e: |
| 56 | raise RuntimeError(f"Invalid JSON schema: {e}") |
| 57 | |
| 58 | # Load and validate the config file |
| 59 | try: |
| 60 | config_data = load_config(config_file) |
| 61 | if verbose: |
| 62 | logging.info(f"Loaded configuration file '{config_file}'") |
| 63 | except Exception as e: |
| 64 | raise RuntimeError(str(e)) |
| 65 | |
| 66 | # Perform validation |
| 67 | validate(instance=config_data, schema=schema) |
| 68 | return True |
| 69 | |
| 70 | |
| 71 | def main(): |