-
-
Notifications
You must be signed in to change notification settings - Fork 141
feat(openapi): form-data helpers #951
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| --- | ||
| title: Form Data Helpers | ||
| description: Utilities for parsing form data and handling validation errors with bracket notation support. | ||
| --- | ||
|
|
||
| # Form Data Helpers | ||
|
|
||
| Form data helpers provide utilities for parsing HTML form data and extracting validation error messages, with full support for [bracket notation](/docs/openapi/bracket-notation) to handle complex nested structures. | ||
|
|
||
| ## `parseFormData` | ||
|
|
||
| Parses HTML form data using [bracket notation](/docs/openapi/bracket-notation) to deserialize complex nested objects and arrays. | ||
|
|
||
| ```ts twoslash | ||
| import { parseFormData } from '@orpc/openapi-client/helpers' | ||
|
|
||
| const form = new FormData() | ||
| form.append('name', 'John') | ||
| form.append('user[email]', 'john@example.com') | ||
| form.append('user[hobbies][]', 'reading') | ||
| form.append('user[hobbies][]', 'gaming') | ||
|
|
||
| const parsed = parseFormData(form) | ||
| // Result: | ||
| // { | ||
| // name: 'John', | ||
| // user: { | ||
| // email: 'john@example.com', | ||
| // hobbies: ['reading', 'gaming'] | ||
| // } | ||
| // } | ||
| ``` | ||
|
|
||
| ## `getIssueMessage` | ||
|
|
||
| Extracts validation error messages from [standard schema](https://github.com/standard-schema/standard-schema) issues using [bracket notation](/docs/openapi/bracket-notation) paths. | ||
|
|
||
| ```ts twoslash | ||
| import { getIssueMessage } from '@orpc/openapi-client/helpers' | ||
|
|
||
| const error = { | ||
| data: { | ||
| issues: [ | ||
| { | ||
| path: ['user', 'email'], | ||
| message: 'Invalid email format' | ||
| } | ||
| ] | ||
| } | ||
| } | ||
|
|
||
| const emailError = getIssueMessage(error, 'user[email]') | ||
| // Returns: 'Invalid email format' | ||
|
|
||
| const tagError = getIssueMessage(error, 'user[tags][]') | ||
| // Returns error message for any array item | ||
|
|
||
| const anyError = getIssueMessage('anything', 'path') | ||
| // Returns undefined if cannot find issue | ||
| ``` | ||
|
|
||
| ::: warning | ||
| The `getIssueMessage` utility works with any data type but requires validation errors to follow the [standard schema issue format](https://github.com/standard-schema/standard-schema?tab=readme-ov-file#the-interface). It looks for issues in the `data.issues` property. If you use custom [validation errors](/docs/advanced/validation-errors), store them elsewhere, or modify the issue format, `getIssueMessage` may not work as expected. | ||
| ::: | ||
|
|
||
| ## Usage Example | ||
|
|
||
| ```tsx | ||
| import { getIssueMessage, parseFormData } from '@orpc/openapi-client/helpers' | ||
|
|
||
| export function ContactForm() { | ||
| const [error, setError] = useState() | ||
|
|
||
| const handleSubmit = (form: FormData) => { | ||
| try { | ||
| const data = parseFormData(form) | ||
| // Process structured data | ||
| } | ||
| catch (error) { | ||
| setError(error) | ||
| } | ||
| } | ||
|
|
||
| return ( | ||
| <form action={handleSubmit}> | ||
|
dinwwwh marked this conversation as resolved.
|
||
| <input name="user[name]" type="text" /> | ||
| <span>{getIssueMessage(error, 'user[name]')}</span> | ||
|
|
||
| <input name="user[emails][]" type="email" /> | ||
| <span>{getIssueMessage(error, 'user[emails][]')}</span> | ||
|
|
||
| <button type="submit">Submit</button> | ||
| </form> | ||
| ) | ||
| } | ||
| ``` | ||
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| it('exports something', async () => { | ||
| expect(await import('./index')).toHaveProperty('getIssueMessage') | ||
| }) |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export { getIssueMessage, parseFormData } from '../adapters/standard' |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.