feat: extend complex formdata support to StandardSchema #1697
Open
Teyik0 wants to merge 17 commits intoelysiajs:mainfrom
Open
feat: extend complex formdata support to StandardSchema #1697Teyik0 wants to merge 17 commits intoelysiajs:mainfrom
Teyik0 wants to merge 17 commits intoelysiajs:mainfrom
Conversation
…orms
Adds support for nested file uploads in multipart forms using dot notation.
This enables organizing files and data in nested structures while maintaining
compatibility with standard multipart/form-data encoding.
Changes:
- Add setNestedValue helper to reconstruct nested objects from dot notation
- Update multipart parser in dynamic-handle.ts (3 locations)
- Update code generation in web-standard adapter
- Bun adapter inherits changes automatically via WebStandardAdapter
- Add comprehensive tests for nested file uploads
- Add example demonstrating nested file upload patterns
Key features:
- Works with any validation library (universal support)
- Zero overhead for flat forms (fast path preserved)
- Handles deeply nested structures (e.g., "user.profile.photo")
- Supports mixed flat and nested fields
- Type-safe with TypeBox schemas
Client usage:
const formData = new FormData()
formData.append('user.name', 'John')
formData.append('user.avatar', fileBlob)
// Server receives: { user: { name: 'John', avatar: File } }
Related to nested object coercing that already handles nested objects
by stringifying them - this extends that pattern to support File objects
which cannot be stringified.
- Add phantom type markers (__elysia_objectstring__, __elysia_arraystring__) to ObjectString and ArrayString types for future Eden auto-detection - Make ObjectString.Decode() accept both JSON strings and already-decoded objects - Enables both stringify (compact) and dot notation (universal) strategies to work seamlessly - Backward compatible: all existing tests pass - Prepares for Eden smart auto-detection feature
…rt-eF4Rq feat: add nested file upload support with dot notation in multipart forms
Extended the nested object reconstruction to handle array indices in multipart
form keys (e.g., `images.create[0]`, `items[1].name`).
Changes:
- Updated setNestedValue() to parse and handle array index notation [N]
- Arrays are automatically initialized when indices are detected
- Code generation in web-standard adapter updated for array support
- Added comprehensive test for complex nested arrays with files
This enables schemas like:
t.Object({
images: t.Object({
create: t.Files(),
update: t.Array(t.Object({
id: t.String(),
img: t.File()
}))
})
})
All 49 body validator tests pass.
…rt-eF4Rq feat: add array index support in nested multipart form parsing
Block dangerous keys (__proto__, constructor, prototype) to prevent prototype pollution attacks in nested file upload feature. Changes: - Add validation in setNestedValue() to block dangerous keys - Add protection in generated multipart parser code - Add security tests for prototype pollution scenarios Security Impact: - Prevents attackers from injecting properties into Object.prototype - Blocks pollution via dot notation (e.g., user.__proto__.isAdmin) - Blocks pollution via array notation (e.g., items[__proto__]) Tests: - 53 existing tests pass - 3 new security tests added - Zero performance impact Fixes security issues identified by cubic-dev-ai bot in PR #4
Improve nested formdata reliability
Contributor
WalkthroughAdds nested/dot-notation multipart/form-data parsing and normalization, prototype-pollution guards, adapter/handler integration for nested arrays/objects and files, a new example, and extensive tests for nested form handling and security. Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant Adapter as WebStandardAdapter
participant Handler as DynamicHandler
participant Validator
participant Route as RouteHandler
Client->>Adapter: POST multipart/form-data (dot-notation keys, files)
Adapter->>Adapter: parse FormData, detect arrays/JSON, guard dangerous keys
Adapter->>Handler: pass normalized key/value map
Handler->>Handler: normalizeFormValue -> setNestedValue (handle arrays/files)
Handler->>Validator: validate/ decode body (may be async)
Validator-->>Handler: validated body
Handler->>Route: invoke route handler with nested body
Route-->>Client: response
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
commit: |
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.
My precedent works on complex formdata was not complete, this one should end my journey around complex formdata and put Elysia in a good state regarding limited support on this on others frameworks.
This PR has two primary goals:
Feature
Clients can now combine JSON stringify with dot notation to optimize bandwidth and support complex nested structures with files:
example ->
No breaking change, fully tested with complex example (maybe too much).
I think that all edge case are handled.
Next-step (if review ok), update Eden to handle nested image as well.
Summary by CodeRabbit
New Features
Improvements
Tests
✏️ Tip: You can customize this high-level summary in your review settings.