Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validating False #2

Closed
matiasmorant opened this issue Nov 5, 2017 · 1 comment
Closed

Validating False #2

matiasmorant opened this issue Nov 5, 2017 · 1 comment
Labels

Comments

@matiasmorant
Copy link

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
...
@spy16
Copy link
Owner

spy16 commented Nov 6, 2017

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.

@spy16 spy16 added the question label Nov 17, 2017
@spy16 spy16 closed this as completed Nov 17, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants