Skip to main content

Module validate

Module validate 

Source
Expand description

Whether a value fits a JSON schema, and where it does not: the check a tool call’s arguments pass before the tool runs (arguments_refusal).

use rig_core::tool::validate::{Problem, validate};
use serde_json::json;

let schema = json!({
    "type": "object",
    "properties": {"path": {"type": "string"}, "limit": {"type": "integer"}},
    "required": ["path"],
});
assert_eq!(validate(&schema, &json!({"path": "a.rs", "limit": 3})), Ok(()));
let problems = validate(&schema, &json!({"limit": "3"})).unwrap_err();
assert_eq!(
    problems.iter().map(Problem::to_string).collect::<Vec<_>>(),
    ["/limit: expected an integer, got a string", "/path: missing"],
);

It checks a subset of JSON Schema, in plain serde_json:

  • type (a name or a list; integer is a number with no fraction), enum and const;
  • properties, required and additionalProperties (false or a schema);
  • items (a schema, or a list by position as in draft-07), prefixItems, minItems and maxItems;
  • minLength and maxLength, in characters;
  • minimum, maximum, exclusiveMinimum and exclusiveMaximum;
  • allOf, anyOf and oneOf (exactly one);
  • $ref to any local JSON pointer (#, #/$defs/…, draft-07’s #/definitions/…), resolved against the root schema. A remote or unresolvable $ref, or one that loops back without reaching a value, accepts the value there.

A null for a declared property the object does not require counts as absent, as serde reads an Option field: args_schema declares an Option optional, not nullable, and a model may still send null for it.

Every other keyword is ignored, so it accepts: pattern, format, patternProperties (and so additionalProperties beside it, since which keys a pattern allows is not checked), and any keyword not listed.

Structs§

Problem
Where a value does not fit a schema, and how.

Functions§

validate
Checks value against schema: Ok when it fits; otherwise every problem, in document order. The module docs list what is checked.