schema
schema copied to clipboard
Feature request: "validatables" that simply returns what you pass in
I had a use case where I wanted to return a certain value when one was passed. For example I had this input:
{'power': 'on'}
And I wanted this output:
{'power': '~PN\r'} # that's a command for a serial interface
So I created a schema like this:
s = Schema({
'power': And('on', Return('~PN\r'))
})
And Return is:
class Return(object):
def __init__(self, value, error=None):
self._value = value
def __repr__(self):
return '%s(%r)' % (self.__class__.__name__, self._value)
def validate(self, data):
return self._value
Does that make sense? Is there a simpler way to do this? And is this even in the scope of this project?
Btw., I know I could have done this: Use(lambda x: '~PN\r') but it felt so clunky and unreadable.
This is interesting. I never had such need myself, but could be useful. Let's wait and see if someone else finds this useful (and comments here).
This would be solved if Use accepted literals in addition of callables.
@agutc interesting, but then you would only be able to use literals and simple values in that case.