marona 2.0.0

Universal Model Gateway and MCP/Skill SDK with mandatory project-scoped Marona authentication
Documentation
use crate::{Error, Result};
use serde_json::Value;

pub(crate) fn validate(value: &Value, schema: &Value, stage: &str) -> Result<()> {
    reject_remote_refs(schema)?;
    let validator = jsonschema::options()
        .with_draft(jsonschema::Draft::Draft202012)
        .build(schema)
        .map_err(|_| Error::Validation("Invalid JSON Schema".into()))?;
    if !validator.is_valid(value) {
        return Err(Error::Validation(format!(
            "{stage} does not match its JSON Schema"
        )));
    }
    Ok(())
}

fn reject_remote_refs(value: &Value) -> Result<()> {
    match value {
        Value::Object(node) => {
            for key in ["$ref", "$dynamicRef", "$recursiveRef"] {
                if let Some(reference) = node.get(key) {
                    if !reference.as_str().is_some_and(|v| v.starts_with('#')) {
                        return Err(Error::Validation(
                            "Only local JSON Schema references are supported".into(),
                        ));
                    }
                }
            }
            for child in node.values() {
                reject_remote_refs(child)?;
            }
        }
        Value::Array(items) => {
            for item in items {
                reject_remote_refs(item)?;
            }
        }
        _ => {}
    }
    Ok(())
}