Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

343
Views
How to over-write the pydantic model's validation message

I want to change the validation message from pydantic model class, code for model class is below:

class Input(BaseModel):
    ip: IPvAnyAddress

    @validator("ip", always=True)
    def not_valid_ip(cls, v):
        """To validate ip-address."""
        if str(v) == "":
            raise ValueError(f'Invalid IP-Address:: {v}')
        if not isinstance(v, IPvAnyAddress):
            raise ValueError(f'Invalid IP format:: {v}')
        return v

currently, it does not update the message written above. I am using fastAPI for API development.

{
    "detail": [
        {
            "loc": [
                "body",
                "input",
                "ip"
            ],
            "msg": "value is not a valid IPv4 or IPv6 address",
            "type": "value_error.ipvanyaddress"
        }
    ]
}
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

That's because the standard validation is failing before your validators is called.

All you need to do is add pre=True to your validator decorator, e.g.

@validator("ip", always=True, pre=True)
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!