Pydantic, Django Ninja "Day is out of range for month"

38 Views Asked by At

2024-02-29 14:04:15 pydantic.error_wrappers.ValidationError: 1 validation error for NinjaResponseSchema 2024-02-29 14:04:15 response 2024-02-29 14:04:15 day is out of range for month (type=value_error)

I have fields "created_at" and "updated_at". Today my project crashed because of them. How can I update validators in schemas to skip those fields validation?

I've tried multiple things but nothing helped me. My versions:

django-ninja==0.22.0 pydantic==1.10.14

1

There are 1 best solutions below

0
back--end On

Use Pydantic's conditional validation to selectively validate fields based on a condition:

from pydantic import validator

class MyResponseSchema(Schema):
    created_at: datetime = Field(...)
    updated_at: datetime = Field(...)

    @validator('created_at', 'updated_at', pre=True, always=True)
    def skip_validation_for_specific_dates(cls, v):
        if v == "2024-02-29":  
            return v 
        return parse_datetime(v)  

but before you have to install pydantic with this :

pip install pydantic