Open severo opened 4 years ago
See also https://federicoferoldi.com/2017/12/28/using-the-typescript-type-system-to-validate-express-handlers.html that relies on that idea.
Thanks @severo, this is an interesting idea!
This is something that should be exposed from api-shema-builder, as this is the package that is responsible for parsing the definitions file.
We'll try to see what we can do when we'll have the time, but of course PRs are always welcome.
Since it's possible to generate typescript definitions from an OpenAPI specification (ie with https://www.npmjs.com/package/dtsgenerator), I wonder if it would be possible to implement type safety between the validator middleware and the following middlewares.
For example, dtsgenerator would transform https://github.com/PayU/openapi-validator-middleware/blob/master/test/pet-store-swagger.yaml to the following types declarations file:
Ideally, the validator would return a request with the expected type (something like
Request<{}, {}, Paths.CreatePets.Parameters.Body>
, see https://stackoverflow.com/a/55413670/7351594), so that the following middleware / route handler would know the type of itsreq
argument:That way, the following route handler would not have to assume validation has already been done, thus reducing code coupling.