feat: add serializer context + validateResponse option#315
Merged
Conversation
Contributor
Coverage Report
File Coverage
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
commit: |
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.
845ea93 to
5453629
Compare
- 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Two features and a follow-up review fix commit.
1. Serializer Context (
SerializerContext)Adds a
SerializerContexttype withfiltersandoptionsfields, passed as a second parameter to the serializer function in all 5 auto endpoints. The serializer signature changes from(obj: object) => objectto(obj: object, context?: SerializerContext) => object— fully backward-compatible since the parameter is optional.Context passed per endpoint type:
ListEndpoint/ReadEndpoint{ filters, options }UpdateEndpoint/DeleteEndpoint{ filters }CreateEndpoint{ filters: [] }2. Response Body Validation (
validateResponse)Adds a
validateResponserouter 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).200schemaResponseobjects withapplication/jsoncontent: cloned, validated against matching status schema, reconstructed500(code7013) with details logged viaconsole.errorResponseValidationException(status 500, code 7013,isVisible: false)3. Review Fixes
CreateEndpoint: passes{ filters: [] }instead of{}for consistent contextSerializerContext.options: tightened type with specificpage/per_page/order_by/order_by_directionkeysResponseValidationException: acceptsmessage+ErrorOptionswithcausefor better debuggingvalidateResponse: distinguishes JSON parse errors from Zod validation errors in logsgetSchemaZodandvalidateResponseModeltype definition, serializer examples, and prose inbase.mdandadvanced-topics-patterns.md