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

Use(bool) doesn't properly validate boolean strings. #266

Open
EdEngle opened this issue Sep 21, 2021 · 1 comment
Open

Use(bool) doesn't properly validate boolean strings. #266

EdEngle opened this issue Sep 21, 2021 · 1 comment

Comments

@EdEngle
Copy link

EdEngle commented Sep 21, 2021

Use(bool) will return True for strings that should be false.

t = Schema({"test_bool": Use(bool)})
t.validate({"test": "false"})

the code above will return {'test': True}

I would recommend using distutils.util.strtobool to convert strings to booleans. This will convert things like "TRUE", "True", "true", "T", 1 to True and it will convert "FALSE", "False", "false", "F", "f", 0 to False.

@EdEngle
Copy link
Author

EdEngle commented Sep 21, 2021

FYI to those wondering you can do this....

def convert_bool(input_bool):
    return bool(strtobool(str(input_bool)))

t = Schema({"test_bool": Use(convert_bool)})
t.validate({"test": "false"})

And that will evaluate test to False correctly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant