aiorpc icon indicating copy to clipboard operation
aiorpc copied to clipboard

Way to enforce argument types and optional arguments

Open volfco opened this issue 6 years ago • 0 comments

I think it would be awesome if there was a way to define what kind of input a function takes, if it's required, and a way to enforce the type (i.e. str, int). Of the top of my head, we could do something like this:

@aiorpc.method({
    'id': [str, True],
    'ttl': [int, False, 86400]
})
async def set_ttl(payload):
    ...

All decorated functions would be registered as methods, and the provided dict would list the name of the arguments, and then the value would be a list- Type, Required, Default (optional). On the backend we could implement our own validation logic or build json schema objects on the fly.

I see two advantages:

  1. A way to abstract away from the requirement of implementing validation logic inside each function.
  2. We can have optional arguments in methods (which is always cool)

Thoughts?

volfco avatar Sep 20 '18 04:09 volfco