Ge0rg3 / flask-parameter-validation

Get and validate all Flask input parameters with ease.
https://pypi.org/project/Flask-Parameter-Validation/
29 stars 12 forks source link

Allow datetime Input Type #3

Closed ghost closed 2 years ago

ghost commented 2 years ago

Hi everyone! I'm trying to validate a date input. My code is:

@ValidateParameters()
def ficha3_download(
            cnpj: str = Query(max_str_length=18),
            cod_produto: str = Query(),
            data_inicio: datetime = Query(),
            data_final: datetime = Query()
            ):

and the input date is for example "2020-01-01". I keep getting the error {"error":"Parameter 'data_inicio' must be type 'datetime'"} Also tried with date type but got the same error. Does anyone know how to handle this type of validation?

Ge0rg3 commented 2 years ago

Hi @robson-araujo-solvimm! Thank you for the issue. Unfortunately, the library doesn't currently support datetime, but I'll add that to the roadmap for the next release 😄

In the mean time, would something like this work for you?:

@ValidateParameters()
def ficha3_download(
            cnpj: str = Query(max_str_length=18),
            cod_produto: str = Query(),
            data_inicio: str = Query(regex="\d{4}-\d{2}-\d{2}"),
            data_final: str = Query(regex="\d{4}-\d{2}-\d{2}")
            ):
        # Convert data_inicio and data_final to datetime object from str manually
ghost commented 2 years ago

Thank you @Ge0rg3 for the fast reply! I'll use your suggestion.

Ge0rg3 commented 2 years ago

Will keep open as a self-reminder to implement datetime parsing :)

Ge0rg3 commented 2 years ago

Closed in #9!