spy16 / pyschemes

PySchemes is a library for validating data structures in python

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Validating False

matiasmorant opened this issue · comments

What if we want to validate the value False (and other values which evaluate to False, like "" [] and 0)?
As in your example :

>>> def CustomValidator(value):
...    if value == False or value in range(100):
...       return value
...    else:
...       return False
...

CustomValidator is a function validator which is converted to type CallableValidator when passed as Scheme(CustomValidator).. If you need to validate for this kind of cases, you can directly use CallableValidator with fail_flag set to custom indicator value.

class FailureIndicator(object): pass

failure = FailureIndicator()

def CustomValidator(value):
    if value == False or value in range(100):
        return value
    else:
        return failure

# then validate as
Scheme(pyschemes.CallableValidator(CustomValidator, fail_flag=failure)).validate(False)
# Output: False

Now, the validation will fail if and only if CustomValidator function returns value failure. False values like False, empty iterables etc. will be validated without error.