use boon::{Compiler, Schemas};
use serde_json::Value;
pub fn validate(schema: Value, instance: &Value) -> Result<(), String> {
const HERE: &str = "https://tailscale-mcp.invalid/vendored.schema.json";
let mut schemas = Schemas::new();
let mut compiler = Compiler::new();
compiler
.add_resource(HERE, schema)
.expect("the vendored schema compiles");
let compiled = compiler
.compile(HERE, &mut schemas)
.expect("the vendored schema compiles");
schemas
.validate(instance, compiled)
.map_err(|error| format!("{error:#}"))
}
pub fn json_at(path: std::path::PathBuf) -> Value {
let text =
std::fs::read_to_string(&path).unwrap_or_else(|why| panic!("{}: {why}", path.display()));
serde_json::from_str(&text)
.unwrap_or_else(|why| panic!("{} is not JSON: {why}", path.display()))
}
pub fn refuses(schema: &Value, cases: impl IntoIterator<Item = (&'static str, Value)>) {
for (what, broken) in cases {
assert!(
validate(schema.clone(), &broken).is_err(),
"{what} should have been caught"
);
}
}