Skip to content

[feature] Response schema validation (JSON Schema / OpenAPI ref) #41

Description

@rwrife

Pitch

We already import OpenAPI 3.x specs (#25) and support # @test assertions (#6). Close the loop: let devs assert responses against a schema instead of hand-writing per-field assertions. Two entry points, one core:

  1. Inline JSON Schema — # @schema { "type": "object", "required": ["id"] }
  2. Reference a schema from an imported OpenAPI file — # @schema openapi:./api.yaml#/paths/~1users~1{id}/get/responses/200

Assertion runs after the response comes back; failures show up in the response panel with the exact JSON Pointer path and a plain-English reason.

Why this matters

  • API contract drift is the [M1] Scaffold VS Code extension + send a single request #1 silent bug source. Reqit already has the response in hand; validating it is table-stakes for anyone wiring .http files into CI ([M6] Run-file mode + test assertions + CLI #6 CLI mode).
  • Zero new heavy deps: use ajv (already common in the TS ecosystem, MIT-licensed, tiny footprint, we already validate everything with zodajv earns its keep because it is the JSON Schema validator).
  • Reuses the existing OpenAPI import path — no second parser.

User-visible behavior

  • Add # @schema <inline-json | openapi:<path>#/<pointer> | file:<path>> directive above a request.
  • On successful response, validate response.body against the resolved schema.
  • Failures rendered in a new Schema tab in the response panel:
    • Bulleted list of violations (JSON Pointer + rule + expected/actual)
    • Copy-as-issue-body button
  • Passes: green checkmark badge on the response panel header.
  • CLI (reqit run) exits non-zero on schema-validation failure — same as @test failures — so this is drop-in for CI gates.

Acceptance criteria

  • Pure src/core/schemaValidator.ts: resolves a schema ref (inline / file / openapi-pointer), validates a JSON body with ajv, returns a normalized { ok, violations[] } result. Unit-tested with vitest (inline pass/fail, ref resolution, invalid-pointer errors, non-JSON response guard).
  • Pure src/core/schemaRef.ts: JSON Pointer + OpenAPI $ref resolver (walks components.schemas and paths.*.responses.*.content['application/json'].schema). Unit-tested.
  • Parser (src/core/parser.ts) recognizes # @schema directive; validated by zod; ignored on non-JSON responses with a warning.
  • Response panel gets a Schema tab (only shown when a @schema was set) with pass/fail + violations list.
  • CLI: schema-validation failure participates in the run summary + non-zero exit.
  • README: short "Schema validation" section with an inline example and an OpenAPI-ref example.
  • Dependency addition: ajv + ajv-formats (only these; no ajv-cli, no json-schema-ref-parser).

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions