Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i set max string field length constraint in pydantic?

So, i have pydantic model with string field

class MyPydanticModel(BaseModel):
    name: Optional[str]

And i want make max length for this field is 10. How can i do this?

like image 592
Ezerzez Avatar asked Apr 20 '20 15:04

Ezerzez


1 Answers

You can use constr:

from pydantic import BaseModel, constr

class MyPydanticModel(BaseModel):
    name: Optional[constr(max_length=10)]
like image 166
vishes_shell Avatar answered Sep 26 '22 02:09

vishes_shell