Skip to content

Commit

Permalink
Merge pull request #6 from pthegner/patch-2
Browse files Browse the repository at this point in the history
Update boolean types
  • Loading branch information
dvdn authored Mar 12, 2020
2 parents 7f0c4ef + d31a63c commit 5e58df1
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions flask_restful/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,24 @@ def natural(value):


def boolean(value):
"""Parse the string "true" or "false" as a boolean (case insensitive)"""
"""Parse the string ``"true"`` or ``"false"`` as a boolean (case
insensitive). Also accepts ``"1"`` and ``"0"`` as ``True``/``False``
(respectively). If the input is from the request JSON body, the type is
already a native python boolean, and will be passed through without
further parsing.
"""
if isinstance(value, bool):
return value

if isinstance(value, int):
return bool(value)

if not value:
raise ValueError("boolean type must be non-null")
value = value.lower()
if value == 'true':
if value in ('true', '1'):
return True
if value == 'false':
if value in ('false', '0'):
return False
raise ValueError("Invalid literal for boolean(): {}".format(value))

Expand Down

0 comments on commit 5e58df1

Please sign in to comment.