|
| 1 | +# 402 - Json Schema Structure Validator |
| 2 | + |
| 3 | +```rig |
| 4 | +import { agent, p, s, repair, defineTool } from "rig"; |
| 5 | +
|
| 6 | +const validateStructure = defineTool("validateStructure", { |
| 7 | + description: "Validate a JSON data file against a JSON schema file, checking required fields and types.", |
| 8 | + parameters: s.object({ schemaContent: s.string, dataContent: s.string }), |
| 9 | + handler: async ({ schemaContent, dataContent }: { schemaContent: string; dataContent: string }) => { |
| 10 | + const errors: string[] = []; |
| 11 | + let schema: Record<string, unknown>; |
| 12 | + let data: Record<string, unknown>; |
| 13 | + try { |
| 14 | + schema = JSON.parse(schemaContent) as Record<string, unknown>; |
| 15 | + } catch (e) { |
| 16 | + return { valid: false, errors: ["Invalid JSON in schema file"], checkedFields: 0, schemaTitle: undefined }; |
| 17 | + } |
| 18 | + try { |
| 19 | + data = JSON.parse(dataContent) as Record<string, unknown>; |
| 20 | + } catch (e) { |
| 21 | + return { valid: false, errors: ["Invalid JSON in data file"], checkedFields: 0, schemaTitle: undefined }; |
| 22 | + } |
| 23 | + const required = Array.isArray(schema["required"]) ? (schema["required"] as string[]) : []; |
| 24 | + const properties = (schema["properties"] ?? {}) as Record<string, { type?: string }>; |
| 25 | + let checkedFields = 0; |
| 26 | + for (const key of required) { |
| 27 | + checkedFields++; |
| 28 | + if (!(key in data)) { |
| 29 | + errors.push(`Missing required field: ${key}`); |
| 30 | + } else if (properties[key]?.type) { |
| 31 | + const expected = properties[key].type as string; |
| 32 | + const actual = Array.isArray(data[key]) ? "array" : typeof data[key]; |
| 33 | + if (actual !== expected) { |
| 34 | + errors.push(`Field "${key}" expected type "${expected}" but got "${actual}"`); |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + return { |
| 39 | + valid: errors.length === 0, |
| 40 | + errors, |
| 41 | + checkedFields, |
| 42 | + schemaTitle: typeof schema["title"] === "string" ? schema["title"] : undefined, |
| 43 | + }; |
| 44 | + }, |
| 45 | +}); |
| 46 | +
|
| 47 | +// Agent role: Validate a JSON data file against a JSON schema and report structural errors. |
| 48 | +const jsonSchemaStructureValidator = agent({ |
| 49 | + model: "small", |
| 50 | + input: s.object({ schemaFile: s.path, dataFile: s.path }), |
| 51 | + instructions: p`Schema file content: |
| 52 | +${p.readInput("schemaFile")} |
| 53 | +
|
| 54 | +Data file content: |
| 55 | +${p.readInput("dataFile")} |
| 56 | +
|
| 57 | +Use the validateStructure tool with both file contents to check required fields and types. Return the validation result.`, |
| 58 | + tools: [validateStructure], |
| 59 | + output: s.object({ |
| 60 | + valid: s.boolean, |
| 61 | + errors: s.array(s.string), |
| 62 | + checkedFields: s.int, |
| 63 | + schemaTitle: s.optional(s.string), |
| 64 | + }), |
| 65 | + addons: [repair()], |
| 66 | +}); |
| 67 | +
|
| 68 | +export default jsonSchemaStructureValidator; |
| 69 | +``` |
0 commit comments