pulsatrix 1.1.0
Loading...
Searching...
No Matches
json_schema.hpp File Reference

A small JSON Schema validator for tool inputs. More...

#include <cstddef>
#include <string>
#include <vector>
#include "pulsatrix/json.hpp"
Include dependency graph for json_schema.hpp:

Go to the source code of this file.

Classes

struct  pulsatrix::orch::SchemaError
 One way a value fails a schema. More...
 

Namespaces

namespace  pulsatrix
 
namespace  pulsatrix::orch
 

Functions

void pulsatrix::orch::CheckSchema (const JsonValue &schema)
 Checks that schema uses only the supported keywords, each with a valid value.
 
std::vector< SchemaError > pulsatrix::orch::ValidateJson (const JsonValue &schema, const JsonValue &value, std::size_t max_errors=16)
 Checks value against schema.
 
std::string pulsatrix::orch::StrictModeProblem (const JsonValue &schema)
 Why schema can't be used with Anthropic's strict tool use, or "" if it can.
 
bool pulsatrix::orch::JsonEquals (const JsonValue &a, const JsonValue &b)
 JSON equality, as enum, const and uniqueItems use it: numbers by value (1 equals 1.0), objects regardless of member order.
 

Detailed Description

A small JSON Schema validator for tool inputs.

It supports only the keywords that mean the same in JSON Schema draft-7 and 2020-12, so one schema works with the Anthropic API, OpenAI-compatible servers and MCP clients, whichever draft they assume:

  • type (a name or an array of names), enum, const
  • objects: properties, required, additionalProperties, minProperties, maxProperties
  • arrays: items (one schema for every element), minItems, maxItems, uniqueItems
  • strings: minLength, maxLength (in Unicode code points)
  • numbers: minimum, maximum, exclusiveMinimum, exclusiveMaximum (numbers), multipleOf
  • combining: allOf, anyOf, oneOf, not
  • annotations, which are accepted and not checked: title, description, default, examples, format, $comment, and $schema naming draft-7 or 2020-12

Any other keyword is refused when the schema is checked, rather than silently not enforced. That includes $ref, $defs/definitions and dependencies, whose spelling or meaning changed between the drafts, items as an array (2020-12's prefixItems), and pattern, since a regular expression engine that is safe on hostile input is more than this needs.