Validate an agent's tool input
Give a model a tool's JSON Schema from the same truefit schema that validates its arguments, and send the issues back as the tool result, so the model reads a JSON Pointer and a fix line and corrects its own call.
A tool an agent calls needs two things from one schema: a JSON Schema to advertise, and a validator for what the model actually sends. When the call is wrong, the most useful tool result is the issue list, because each message says what to send instead.
import { enumOf, issues, number, object, optional, string } from 'truefit';
import { toJsonSchema } from 'truefit/json-schema';
const SearchArgs = object(
{
query: string({ min: 1, max: 200 }),
limit: optional(number({ int: true, min: 1, max: 50 })),
sort: optional(enumOf(['relevance', 'date'])),
},
{ unknown: 'strict' },
);
const tool = { name: 'search', input_schema: toJsonSchema(SearchArgs).schema };
console.log(JSON.stringify(tool.input_schema.required));
export function callSearch(args) {
const found = issues(SearchArgs, args);
if (found.length > 0) return { is_error: true, content: found.map((i) => `${i.path} ${i.message}`).join('\n') };
return { content: `searching for ${args.query}` };
}
console.log(callSearch({ query: 'truefit', limit: 500, sort: 'newest', page: 2 }).content);["query"]
/limit expected a number of at most 50, got number 500. fix: send 50 or less
/sort expected one of "relevance", "date", got string "newest". fix: send one of them
/page expected no such key, got number 2. fix: remove "page"toJsonSchema returns its findings too. If it returns any, the advertised schema says less than the
validator checks, and the model will only learn the rest from an issue, so check findings is empty
for a tool's schema in a test.
Validate a request body
Validate a JSON request body in any Node server, strip keys the API does not know, answer 400 with every issue as JSON Pointer paths a client can map onto its form fields, and keep the handler typed.
Under a strict Content-Security-Policy
Run validation in a browser extension, an Electron renderer or a page whose Content-Security-Policy forbids unsafe-eval: truefit's check needs no eval, and truefit/emit gives you compiled guards as source at build time.