Skip to content

feat: add serializer context + validateResponse option#315

Merged
G4brym merged 2 commits intomainfrom
feat/serializer-context
Feb 26, 2026
Merged

feat: add serializer context + validateResponse option#315
G4brym merged 2 commits intomainfrom
feat/serializer-context

Conversation

@G4brym
Copy link
Member

@G4brym G4brym commented Feb 25, 2026

Summary

Two features and a follow-up review fix commit.

1. Serializer Context (SerializerContext)

Adds a SerializerContext type with filters and options fields, passed as a second parameter to the serializer function in all 5 auto endpoints. The serializer signature changes from (obj: object) => object to (obj: object, context?: SerializerContext) => object — fully backward-compatible since the parameter is optional.

Context passed per endpoint type:

Endpoint Context
ListEndpoint / ReadEndpoint { filters, options }
UpdateEndpoint / DeleteEndpoint { filters }
CreateEndpoint { filters: [] }
_meta = {
  model: {
    schema: MySchema,
    serializer: (obj, context) => {
      // context.filters — the active FilterCondition[] for this request
      // context.options — pagination/ordering options (list/read endpoints)
      if (context?.filters?.some(f => f.field === "expand")) {
        return expandRelations(obj);
      }
      return obj;
    },
  },
};

2. Response Body Validation (validateResponse)

Adds a validateResponse router option that parses response bodies through their Zod schema at runtime. Strips unknown fields (preventing data leaks) and validates required fields/types (catching handler bugs).

const router = fromHono(app, { validateResponse: true });
  • Plain object responses validated against 200 schema
  • Response objects with application/json content: cloned, validated against matching status schema, reconstructed
  • Non-JSON responses and responses without a Zod schema pass through unchanged
  • Validation failures return 500 (code 7013) with details logged via console.error
  • New export: ResponseValidationException (status 500, code 7013, isVisible: false)

3. Review Fixes

  • CreateEndpoint: passes { filters: [] } instead of {} for consistent context
  • SerializerContext.options: tightened type with specific page/per_page/order_by/order_by_direction keys
  • ResponseValidationException: accepts message + ErrorOptions with cause for better debugging
  • validateResponse: distinguishes JSON parse errors from Zod validation errors in logs
  • JSDoc improvements on getSchemaZod and validateResponse
  • Docs updated: Model type definition, serializer examples, and prose in base.md and advanced-topics-patterns.md
  • Changeset expanded with type shape and per-endpoint table
  • 8 new tests: Create/Update/Delete serializer context, 404 status-specific schema, Zod transform in response

@github-actions
Copy link
Contributor

github-actions bot commented Feb 25, 2026

Coverage Report

Status Category Percentage Covered / Total
🔵 Lines 94.35% 736 / 780
🔵 Statements 94.29% 760 / 806
🔵 Functions 91.71% 155 / 169
🔵 Branches 86.71% 444 / 512
File Coverage
File Stmts Branches Functions Lines Uncovered Lines
Changed Files
src/exceptions.ts 100% 88% 100% 100%
src/route.ts 95.16% 91.02% 100% 96.49% 243, 249, 290-291, 317, 367
src/endpoints/create.ts 94.73% 66.66% 88.88% 94.73% 71
src/endpoints/delete.ts 90% 64.7% 83.33% 90% 81-85, 102
src/endpoints/list.ts 95.12% 82.6% 81.81% 95.12% 41, 146-148
src/endpoints/read.ts 95.83% 85.71% 87.5% 95.83% 97
src/endpoints/types.ts 100% 100% 100% 100%
src/endpoints/update.ts 94.73% 73.07% 81.81% 94.73% 123-127
Generated in workflow #668 for commit 930846f by the Vitest Coverage Report Action

@pkg-pr-new
Copy link

pkg-pr-new bot commented Feb 25, 2026

Open in StackBlitz

npm i https://pkg.pr.new/cloudflare/chanfana@315

commit: 930846f

Pass filters and options as a SerializerContext to the serializer
function in all auto endpoints (create, read, update, delete, list),
enabling context-aware serialization logic based on active filters.
@G4brym G4brym force-pushed the feat/serializer-context branch from 845ea93 to 5453629 Compare February 26, 2026 22:59
- CreateEndpoint: pass { filters: [] } instead of {} for consistent context
- SerializerContext.options: tighten type with specific pagination/ordering keys
- ResponseValidationException: accept message + ErrorOptions with cause
- validateResponse: distinguish JSON parse errors from Zod validation errors
- getSchemaZod: document that default {} schema is relied upon by getResponseSchema
- validateResponse JSDoc: note that body-dependent headers may become stale
- Docs: update Model type, serializer examples, and prose for SerializerContext
- Changeset: expand serializer-context.md with type shape and per-endpoint table
- Tests: add Create/Update/Delete serializer context, 404 schema, Zod transform
@G4brym G4brym changed the title feat: add context parameter to auto endpoint serializer feat: add serializer context + validateResponse option Feb 26, 2026
@G4brym G4brym merged commit 47d304a into main Feb 26, 2026
8 checks passed
@G4brym G4brym deleted the feat/serializer-context branch February 26, 2026 23:38
@github-actions github-actions bot mentioned this pull request Feb 26, 2026
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