schema icon indicating copy to clipboard operation
schema copied to clipboard

Feature request: "validatables" that simply returns what you pass in

Open dAnjou opened this issue 12 years ago • 3 comments

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.

dAnjou avatar Jul 28 '13 21:07 dAnjou

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).

keleshev avatar Jul 29 '13 08:07 keleshev

This would be solved if Use accepted literals in addition of callables.

agutc avatar Aug 30 '13 15:08 agutc

@agutc interesting, but then you would only be able to use literals and simple values in that case.

keleshev avatar Sep 04 '13 15:09 keleshev