pydantic string 'false' to bool

Viewed 630
1 Answers

You should use this construction It will allow you to access properties via dot notation and make custom type checks

class QueryParams:
    def make_bool(self, value):
        return not(value == "false")  # some logic

    def __init__(self, boolable: str, zero: bool):
        self.boolable = self.make_bool(boolable)
        self.zero = zero


@router.get('/route')
async def route(
        data: QueryParams = Depends(),
):
    print(data) # boolable=false : str
    print(type(data.boolable))  # bool
    print("success")
    return Result(data)
Related