opi_agent/validation.rs
1//! JSON Schema validation for tool arguments.
2
3/// Error produced when arguments fail schema validation.
4#[derive(Debug, thiserror::Error)]
5#[error("schema validation failed: {}", errors.join(", "))]
6pub struct ValidationError {
7 pub errors: Vec<String>,
8}
9
10/// Validate `args` against a JSON Schema.
11pub fn validate(
12 schema: &serde_json::Value,
13 args: &serde_json::Value,
14) -> Result<(), ValidationError> {
15 match jsonschema::validate(schema, args) {
16 Ok(()) => Ok(()),
17 Err(e) => Err(ValidationError {
18 errors: vec![e.to_string()],
19 }),
20 }
21}