Skip to content

Commit

Permalink
fix: support array of 1 for query parameter arrays (round 2) (#225)
Browse files Browse the repository at this point in the history
#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.

fixes #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' ] } }
```
  • Loading branch information
mnahkies authored Jul 27, 2024
1 parent ea0d044 commit de3f7eb
Show file tree
Hide file tree
Showing 24 changed files with 2,039 additions and 506 deletions.
22 changes: 16 additions & 6 deletions integration-tests-definitions/todo-lists.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ paths:
schema:
type: string
format: date-time
- name: status
- name: statuses
in: query
schema:
type: string
enum:
- incomplete
- complete
$ref: '#/components/schemas/Statuses'
- name: tags
in: query
schema:
type: array
items:
type:
string
get:
operationId: getTodoLists
responses:
Expand Down Expand Up @@ -174,7 +178,13 @@ components:
updated:
type: string
format: date-time

Statuses:
type: array
items:
type: string
enum:
- incomplete
- complete
parameters:
listId:
name: listId
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit de3f7eb

Please sign in to comment.