openapi-typescript-codegen
openapi-typescript-codegen copied to clipboard
WORK IN PROGRESS: add type guards
Add a type guards i.e. dynamic validation of the JSON returned from the server. This makes the generated code type safe (before it could be missing fields, despite what the TypeScript types say).
This is a WIP PR put here for comments. The general approach I had in mind:
- Add a flag for this feature (e.g.
--useTypeGuards). I'm not attached to the name. - Generate
isModeltype guard function for each generated model. - In the generated service, call
isModelbefore returning the result. Raise some exception if validation fails (or fail some promise). - If the flag is set, remove the unsafe cast to
Modelthat exists today.
Is this feature of interest?
There's still lots to do, mainly made difficult by Handlebars. Type checking is a recursive process (e.g. when validating an array property using a validate partial we'd like to call validate on each element) so we'd like to call a partial with dynamic arguments, which isn't possible in Handlebars AFAIK. I will need to find a workaround.