feat: type inference for check function
#355
Draft
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The PR adds utility types that can infer the TypeScript types from validation schemas.
This is the type inference part that is eventually intended to be used in moleculer to infer the parameter types of action
handlers.I modified the
compile()function of theValidatorso that the returnedcheckfunction expects the TS type that was specified in the schema passed tocompile().If you prefer to not have this implemented with the
check()/compile()function, let me know. We could also just export the utility types for external use (by moleculer).Note that the inference types do not support all features that the validator does. For example,
"email"or"uuid"are interpreted asstring.Also, type inference for validators with the
considerNullAsAValueflag are not implemented (it does not seem to be a very common use case and the complexity would grow. Let me know if you think it should be implemented).This is WIP.
The TS unit tests are not yet adapted (they will require
// @ts-expect-errorcomments forcheck()calls with invalid objects).