Skip to content

feat: content-type support for validateResponse/validateRequest#1

Merged
kevinccbsg merged 11 commits into
mainfrom
feat/content-type-support
Apr 20, 2026
Merged

feat: content-type support for validateResponse/validateRequest#1
kevinccbsg merged 11 commits into
mainfrom
feat/content-type-support

Conversation

@kevinccbsg
Copy link
Copy Markdown
Member

Summary

  • Add optional contentType to ValidatorOptions; defaults to application/json (no breaking changes).
  • Resolve media types in spec with exact-match → family wildcard (image/*) → */*.
  • Silently bypass binary content types (image/video/audio/pdf/zip/octet-stream) when no match — eliminates false MISSING_SCHEMA warnings for binary mocks like QR codes.
  • normalizeAllSchemas now rewrites 3.0 → 3.1 schemas under every media-type entry, not only application/json.

Spec: specs.md (2026-04-20). This is rollout step 1 of 3; twd-cli and twd depend on this.

Test plan

  • npm test — 144 tests pass (9 files)
  • npm run build — clean TypeScript compile
  • Discriminating regression test confirms normalizeAllSchemas actually runs over non-JSON content types (uses 3.0 exclusiveMinimum: true boolean form which Ajv rejects without normalization).
  • Manual smoke: new OpenAPIMockValidator(spec).validateResponse('/qr', 'get', 200, bytes, { contentType: 'image/png' }) returns { valid: true, warnings: [] } when spec declares image/jpeg.

🤖 Generated with Claude Code

kevinccbsg and others added 11 commits April 20, 2026 20:11
Narrow stored options type to { strict: boolean } since contentType
is resolved per-call, not at construction time.
Extend extractResponseSchema with an optional contentType parameter (defaulting
to application/json) that uses isBinaryContentType and resolveMediaType to
support exact, family-wildcard, and */* media-type matching; silently bypasses
unmatched binary types and emits MISSING_SCHEMA for unmatched non-binary types.
Extend extractRequestSchema with an optional contentType parameter (defaulting
to application/json), using resolveMediaType for wildcard matching and
isBinaryContentType for silent bypass of binary payloads.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Plumbs options.contentType (defaulting to 'application/json') from
validateResponse into extractResponseSchema, enabling content-type-aware
schema lookup for binary and wildcard media types.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Update normalizeAllSchemas to iterate over every media-type object in
both response content and requestBody content, rather than only
handling application/json, so non-JSON schemas (multipart/form-data,
text/plain, etc.) are also converted to OpenAPI 3.1 shape during init.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Replace nullable-based assertions (which Ajv silently accepts even
without normalization due to strict:false) with exclusiveMinimum
boolean-form tests that only pass when normalizeAllSchemas actually
rewrites 3.0 schemas under non-JSON content types.
@kevinccbsg kevinccbsg merged commit 7a9172e into main Apr 20, 2026
3 checks passed
@kevinccbsg kevinccbsg deleted the feat/content-type-support branch April 20, 2026 18:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant