You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using typera-express with io-ts, we encountered a situation where the order in which Parser.query is applied seems to affect the type conversion of query parameters.
For reference, we are using the following types for the query parameters:
constCategory=t.string;// Represents a category as a stringconstPagingParams=t.type({page: IntFromString,// Page number, expected to be converted to a numberitemsPerPage: IntFromString// Items per page, also expected to be converted to a number});
Below are examples of three patterns that demonstrate the issue:
Pattern 1 (Working as expected): Using t.intersection to combine category and PagingParams works as intended. Both page and itemsPerPage are correctly converted to numbers.
Pattern 2 (Working as expected): Applying Parser.query separately, first with category and then with PagingParams, also results in correct conversion. Both page and itemsPerPage are properly handled as numbers.
Pattern 3 (Issue): However, when Parser.query(PagingParams) is placed before Parser.query(t.type({ category: t.string })), the page and itemsPerPage parameters remain as strings instead of being converted to numbers. This behavior is unexpected, given that both patterns should logically result in the same output.
In all cases, page and itemsPerPage should be converted to numbers, as they are defined using IntFromString. However, the current behavior suggests that the order of applying Parser.query influences whether the conversion happens correctly, which can be surprising for developers. Since both the approach used in test2 and test3 are valid and commonly used, it would be more intuitive if they behaved consistently, regardless of the order in which the parsers are applied.
The text was updated successfully, but these errors were encountered:
mrsekut
changed the title
Parser.query order affects type conversion in typera-expressParser.query order affects type conversion
Sep 4, 2024
When using
typera-express
withio-ts
, we encountered a situation where the order in whichParser.query
is applied seems to affect the type conversion of query parameters.For reference, we are using the following types for the query parameters:
Below are examples of three patterns that demonstrate the issue:
t.intersection
to combinecategory
andPagingParams
works as intended. Bothpage
anditemsPerPage
are correctly converted to numbers.Parser.query
separately, first withcategory
and then withPagingParams
, also results in correct conversion. Bothpage
anditemsPerPage
are properly handled as numbers.Parser.query(PagingParams)
is placed beforeParser.query(t.type({ category: t.string }))
, thepage
anditemsPerPage
parameters remain as strings instead of being converted to numbers. This behavior is unexpected, given that both patterns should logically result in the same output.Expected Behavior:
In all cases,
page
anditemsPerPage
should be converted to numbers, as they are defined usingIntFromString
. However, the current behavior suggests that the order of applyingParser.query
influences whether the conversion happens correctly, which can be surprising for developers. Since both the approach used intest2
andtest3
are valid and commonly used, it would be more intuitive if they behaved consistently, regardless of the order in which the parsers are applied.The text was updated successfully, but these errors were encountered: