|
| 1 | +# 161 - OpenAPI Spec Validator |
| 2 | + |
| 3 | +```rig |
| 4 | +import { agent, defineTool, p, s } from "rig"; |
| 5 | +
|
| 6 | +// Agent role: validate an OpenAPI spec file for structural correctness and report issues. |
| 7 | +const openapiSpecValidator = agent({ |
| 8 | + model: "small", |
| 9 | + instructions: p`Validate the OpenAPI specification in this workspace. |
| 10 | +
|
| 11 | +openapi.json content: |
| 12 | +${p.readOptional("openapi.json")} |
| 13 | +
|
| 14 | +openapi.yaml content: |
| 15 | +${p.readOptional("openapi.yaml")} |
| 16 | +
|
| 17 | +Use the checkStructure tool to validate the spec content. Check for: required fields (openapi version, info.title, info.version, paths), valid HTTP methods, proper response codes, and schema references. Return only the declared output.`, |
| 18 | + tools: [ |
| 19 | + defineTool("checkStructure", { |
| 20 | + description: "Check structural validity of OpenAPI spec content", |
| 21 | + parameters: s.object({ content: s.string }), |
| 22 | + handler({ content }) { |
| 23 | + const issues: Array<{ type: "error" | "warning" | "info"; message: string; path?: string }> = []; |
| 24 | + try { |
| 25 | + const spec = JSON.parse(content); |
| 26 | + if (!spec.openapi) issues.push({ type: "error", message: "Missing required field 'openapi'", path: "openapi" }); |
| 27 | + if (!spec.info) issues.push({ type: "error", message: "Missing required field 'info'", path: "info" }); |
| 28 | + else { |
| 29 | + if (!spec.info.title) issues.push({ type: "error", message: "Missing info.title", path: "info.title" }); |
| 30 | + if (!spec.info.version) issues.push({ type: "error", message: "Missing info.version", path: "info.version" }); |
| 31 | + } |
| 32 | + if (!spec.paths) issues.push({ type: "error", message: "Missing required field 'paths'", path: "paths" }); |
| 33 | + } catch { |
| 34 | + issues.push({ type: "warning", message: "Content is not valid JSON — may be YAML or empty" }); |
| 35 | + } |
| 36 | + return { issues }; |
| 37 | + }, |
| 38 | + }), |
| 39 | + ], |
| 40 | + output: s.object({ |
| 41 | + valid: s.boolean, |
| 42 | + issues: s.array(s.object({ |
| 43 | + type: s.enum("error", "warning", "info"), |
| 44 | + message: s.string, |
| 45 | + path: s.optional(s.string), |
| 46 | + })), |
| 47 | + issueCount: s.int, |
| 48 | + }), |
| 49 | +}); |
| 50 | +
|
| 51 | +export default openapiSpecValidator; |
| 52 | +``` |
0 commit comments