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

Changing date format #119

Closed
xunto opened this issue Jan 25, 2018 · 4 comments · Fixed by #122
Closed

Changing date format #119

xunto opened this issue Jan 25, 2018 · 4 comments · Fixed by #122

Comments

@xunto
Copy link

xunto commented Jan 25, 2018

What is the right way to do it? I used some monkeypatch:

from pydantic import datetime_parse

datetime_parse.date_re = re.compile(r'(?P<day>\d{1,2}).(?P<month>\d{1,2}).(?P<year>\d{4})$')

but it feels rather hacky. Looks like I can do it with validators but when I return something It still tries to create date from string after that when it's not a string anymore.

class CheckUserRequest(pydantic.BaseModel):
    cardNumber: str
    lastName: str
    birthday: date

    @validator("birthday", pre=True)
    def parse_birthday(cls, value):
        return datetime.strptime(
            value,
            "%d.%m.%Y"
        ).date()

I get:

pydantic.exceptions.ValidationError: error validating input
birthday:
  int() argument must be a string, a bytes-like object or a number, not 'datetime.date' (error_type=TypeError track=date)
@samuelcolvin
Copy link
Member

Not at all sure what you're trying to do.

Please explain a little more.

@samuelcolvin
Copy link
Member

Give the input and expected output.

@xunto
Copy link
Author

xunto commented Jan 29, 2018

@samuelcolvin I use pydantic to validate user request data for my api. I wan't my api to accept another format for dates. Like "%d.%m.%Y" instead of "%Y-%m-%d".

So "23.10.2013" would be a valid date and would be converted to datetime.date.

@samuelcolvin
Copy link
Member

Thanks for reporting the solution here is to fix it properly in pydantic, see #122

alexdrydew pushed a commit to alexdrydew/pydantic that referenced this issue Dec 23, 2023
* avoid creating unnecessary strings

* more Cow usage

* tweaks and cleanup

* tweaks to models
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

Successfully merging a pull request may close this issue.

2 participants