#!/usr/bin/env python3 # DTO Schema Validator # Validates DTO manifests against schema definition import argparse import json import os import sys import yaml from pathlib import Path from typing import Dict, Any class DTOValidator: def __init__(self): self.schema = self._load_schema() def _load_schema(self) -> Dict[str, Any]: """Load DTO schema definition""" schema_path = Path("/data/adaptai/deployment/dto-schema/schema.yaml") if not schema_path.exists(): # Basic schema for initial implementation return { "type": "object", "required": ["manifest", "services"], "properties": { "manifest": { "type": "object", "required": ["version", "domain", "owner"], "properties": { "version": {"type": "string"}, "domain": {"type": "string"}, "owner": {"type": "string"} } }, "services": { "type": "array", "items": { "type": "object", "required": ["name", "description", "owner"], "properties": { "name": {"type": "string"}, "description": {"type": "string"}, "owner": {"type": "string"}, "ports": {"type": "array"}, "health_probes": {"type": "array"}, "supervisor_unit": {"type": "string"}, "slos": {"type": "array"} } } } } } with open(schema_path, 'r') as f: return yaml.safe_load(f) def validate_dto_file(self, dto_path: Path) -> bool: """Validate a single DTO file""" try: with open(dto_path, 'r') as f: dto_content = yaml.safe_load(f) return self._validate_against_schema(dto_content) except Exception as e: print(f"Error validating {dto_path}: {e}") return False def _validate_against_schema(self, dto_content: Dict[str, Any]) -> bool: """Validate DTO content against schema""" # Basic validation logic # In production, use a proper JSON schema validator like jsonschema # Check required top-level fields if "manifest" not in dto_content: print("Missing 'manifest' section") return False if "services" not in dto_content: print("Missing 'services' section") return False manifest = dto_content["manifest"] if not all(field in manifest for field in ["version", "domain", "owner"]): print("Manifest missing required fields: version, domain, or owner") return False # Validate services for service in dto_content["services"]: if not all(field in service for field in ["name", "description", "owner"]): print(f"Service missing required fields: {service.get('name', 'unknown')}") return False print("DTO validation passed") return True def find_dto_files(self) -> List[Path]: """Find all DTO files in the repository""" dto_files = [] # Look in platform/*/dto/ directories platform_dir = Path("/data/adaptai/platform") for domain_dir in platform_dir.iterdir(): if domain_dir.is_dir(): dto_dir = domain_dir / "dto" if dto_dir.exists(): for dto_file in dto_dir.glob("*.yaml"): dto_files.append(dto_file) for dto_file in dto_dir.glob("*.yml"): dto_files.append(dto_file) return dto_files def main(): parser = argparse.ArgumentParser(description="DTO Schema Validator") parser.add_argument("--file", help="Specific DTO file to validate") parser.add_argument("--all", action="store_true", help="Validate all DTO files") args = parser.parse_args() validator = DTOValidator() if args.file: dto_path = Path(args.file) if not dto_path.exists(): print(f"DTO file not found: {args.file}") sys.exit(1) success = validator.validate_dto_file(dto_path) sys.exit(0 if success else 1) elif args.all: dto_files = validator.find_dto_files() all_valid = True for dto_file in dto_files: print(f"Validating {dto_file}...") valid = validator.validate_dto_file(dto_file) if not valid: all_valid = False print(f"❌ Validation failed for {dto_file}") else: print(f"✅ Validation passed for {dto_file}") sys.exit(0 if all_valid else 1) else: parser.print_help() sys.exit(1) if __name__ == "__main__": main()