parent_ai_json_engine 0.0.2

Crate provides a JSON engine for collecting, aggregating, and managing models.
Documentation
use std::collections::HashMap;
use std::path::Path;

pub struct SchemaValidator {
    schemas: HashMap<String, String>,
}

impl SchemaValidator {
    pub fn new(schemas_dir: &Path) -> Result<Self, Box<dyn std::error::Error + Send + Sync>> {
        let mut schemas = HashMap::new();

        let schema_types = vec!["engine_execution", "comparison_result", "training_sample"];
        for schema_type in schema_types {
            let schema_path = schemas_dir.join("v1").join(format!("{}.json", schema_type));
            if let Ok(schema_content) = std::fs::read_to_string(&schema_path) {
                schemas.insert(schema_type.to_string(), schema_content);
            }
        }

        Ok(Self { schemas })
    }

    pub fn validate(&self, data: &str, schema_type: &str) -> Result<(), String> {
        if !self.schemas.contains_key(schema_type) {
            return Err(format!("Unknown schema type: {}", schema_type));
        }
        if data.trim().is_empty() {
            return Err("empty payload".to_string());
        }
        Ok(())
    }

    pub fn validate_or_error(
        &self,
        data: &str,
        schema_type: &str,
    ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
        self.validate(data, schema_type).map_err(|e| e.into())
    }
}