Skip to content
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

fix: support array of 1 for query parameter arrays (round 2) #225

Merged
merged 10 commits into from
Jul 27, 2024

Conversation

mnahkies
Copy link
Owner

@mnahkies mnahkies commented Jul 27, 2024

#220 got a bit off-track with scope creep.

this PR takes a similar approach, but for internal use only

  • detects query parameters with array type schemas
  • adds an internal annotation to indicate these need preprocessing
  • applies a z.preprocess to coerce an individual value to an array of 1 element, before parsing with the array schema
  • for joi we resort to a pretty hacky wrapping of the schema with a object that does the preprocessing, as the joi extension API didn't work as I'd expected it to.

will continue experimenting with making this a more generally useful feature for handling things like parsing date-time strings to be a Date, etc, separately.

relates #217

Testing Notes
Need to improve automated test coverage still, but manually tested on a running server:

  • No query params
  • 1 element
  • 2 elements
  • 2 elements, 1 element

and it seems to be working correctly.

listening on http://127.0.0.1:3000
query { query: {} }
query { query: { statuses: [ 'complete' ] } }
query { query: { statuses: [ 'complete', 'incomplete' ] } }
query { query: { statuses: [ 'complete', 'incomplete' ] } }
query { query: { statuses: [ 'complete', 'incomplete' ], tags: [ '123' ] } }

mnahkies added 10 commits July 27, 2024 09:28
- introduce `x-alpha-transform` concept, allowing for a arbitrary
  transformation function to be applied to a schema
  - not yet supported by `joi`
  - not yet supported by `type-builder`
  - only really useful for this specific case so far
- detect query parameters of an array type, and parse as a `T | T[]`,
  and then transform to a `T[]` for convenience
  - does not yet support `$ref`d schemas properly

relates #217
@mnahkies mnahkies marked this pull request as ready for review July 27, 2024 10:11
@mnahkies mnahkies merged commit de3f7eb into main Jul 27, 2024
1 check passed
@mnahkies mnahkies deleted the mn/217/fix-query-param-arrays-round-2 branch July 27, 2024 10:13
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