mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 23:00:18 +00:00
f55515820a
* renaming .json -> .model_dump_json * renaming .dict -> .model_dump * renaming .__fields__ -> .model_fields * renaming .schema -> .model_json_schema * renaming .construct -> .model_construct * renaming .parse_obj -> .model_validate * make linters happy * add changes md-file Co-authored-by: Samuel Colvin <s@muelcolvin.com>
31 lines
865 B
Python
31 lines
865 B
Python
from pydantic import BaseModel, Field, PositiveInt
|
|
|
|
try:
|
|
# this won't work since PositiveInt takes precedence over the
|
|
# constraints defined in Field meaning they're ignored
|
|
class Model(BaseModel):
|
|
foo: PositiveInt = Field(..., lt=10)
|
|
except ValueError as e:
|
|
print(e)
|
|
|
|
|
|
# but you can set the schema attribute directly:
|
|
# (Note: here exclusiveMaximum will not be enforce)
|
|
class Model(BaseModel):
|
|
foo: PositiveInt = Field(..., exclusiveMaximum=10)
|
|
|
|
|
|
print(Model.model_json_schema())
|
|
|
|
|
|
# if you find yourself needing this, an alternative is to declare
|
|
# the constraints in Field (or you could use conint())
|
|
# here both constraints will be enforced:
|
|
class Model(BaseModel):
|
|
# Here both constraints will be applied and the schema
|
|
# will be generated correctly
|
|
foo: int = Field(..., gt=0, lt=10)
|
|
|
|
|
|
print(Model.model_json_schema())
|