use anyhow::{anyhow, bail, Context, Result};
use serde_json::Value;
pub const SOURCE: &str = include_str!("../schema/ai.schema.json");
pub fn validate(instance: &Value) -> Result<()> {
let schema: Value =
serde_json::from_str(SOURCE).context("embedded ai.json schema is not valid JSON")?;
let compiled = jsonschema::JSONSchema::compile(&schema)
.map_err(|e| anyhow!("embedded ai.json schema is invalid: {e}"))?;
if let Err(errors) = compiled.validate(instance) {
let details: Vec<String> = errors
.map(|e| {
let at = e.instance_path.to_string();
let at = if at.is_empty() { "(root)".into() } else { at };
format!(" at {at}: {e}")
})
.collect();
bail!("ai.json does not match schema:\n{}", details.join("\n"));
}
Ok(())
}