safe-client-gateway
safe-client-gateway copied to clipboard
Only strictly type fetched data after validation
Description
We currently fetch data and explicitly type the response, validating only on the domain layer. Whilst the layer separation makes sense, we can't be 100% sure that the type matches the response until after validation. Ideally, we should assume the response is unknown
until validated.
We should either adjust the types of requests, or validate the response immediately.
Requirements
There are a couple ways to approach this, each adjusting the architecture of the project:
- Change reponse types to
unknown
(or something likeT | unknown
).
The downside of the above is that the types are "lost" and it is hard to then find the corresponding type. A better suggestion would setting all requests as T | unknown
. This is then a) easier as the generic of the service can be changed and not that of every request and b) we always have the relevant type "in place". Validating the response of this will then "remove" then unknown
from the union.
- Immediately validate responses.
We could "hoist" the validation to the datasource layer. If we want to remain "strict", this does not fit our mental model. However, it means that every call returns the expected type but we generally inject the respective repository though. One thing to note is that we are doing something similar with the "incoming data" of DTOs.
Additional information
No response