Pydantic
satya - 3/3/2024, 4:25:02 PM
The BaseModel methods and properties
satya - 3/3/2024, 4:51:22 PM
Pydantic lecture video
satya - 3/3/2024, 5:55:00 PM
Here are some out of the box types
- constr: Defines a string with constraints, such as minimum and maximum length, regex pattern matching, etc.
- conint: Represents an integer with constraints, like a range specified by minimum (ge, gt) and maximum (le, lt) values.
- confloat: A floating-point number with constraints, including minimum, maximum, and whether to allow infinity or NaN values.
- conlist: A list with constraints on its elements' type and the list's minimum and maximum length.
- condict: A dictionary with constraints on key and value types, as well as minimum and maximum size.
- condecimal: A decimal type with constraints such as fixed places and maximum digits.
- EmailStr: A string representing an email address, validated against a standard email regex pattern.
- UrlStr: A string formatted as a URL, validated via regex to ensure it's a well-formed URL.
- Json: A string that must be valid JSON; Pydantic will parse and serialize the JSON automatically.
- SecretStr and SecretBytes: For storing secrets like passwords and tokens. The secret values are not dumped in logs or error messages, enhancing security.
- IPv4Address, IPv6Address, IPvAnyAddress: Represent IPv4, IPv6, or either type of IP address, validated to ensure they are correctly formatted.
- UUID1, UUID4, UUID3, UUID5: Represents UUIDs (Universally Unique Identifiers) of various versions, ensuring they are valid UUID strings.
- FilePath, DirectoryPath, Path: Types for file and directory paths, ensuring the paths exist and optionally that the path is a file or directory.
- PositiveInt, NegativeInt, PositiveFloat, NegativeFloat: Integers or floats constrained to be strictly positive or negative.
- StrictStr, StrictBool, StrictInt, StrictFloat: Types that require values to be explicitly of the correct type, without any of the automatic casting Pydantic usually performs.
satya - 3/3/2024, 5:57:28 PM
you have to first do
pip install pydantic
satya - 3/3/2024, 5:57:46 PM
Set 1
from pydantic import (
BaseModel,
constr,
conint,
confloat,
conlist,
condecimal,
EmailStr,
UrlStr,
Json,
SecretStr,
SecretBytes,
IPv4Address,
IPv6Address,
FilePath,
DirectoryPath,
Path,
PositiveInt,
NegativeInt,
PositiveFloat,
NegativeFloat,
StrictStr,
StrictBool,
StrictInt,
StrictFloat,
)
satya - 3/3/2024, 5:58:02 PM
set 2
from pydantic.types import UUID1, UUID3, UUID4, UUID5
from pydantic.networks import IPv4Address, IPv6Address, IPvAnyAddress
satya - 3/3/2024, 6:00:20 PM
The types are somewhat documented here
satya - 3/3/2024, 6:02:10 PM
For EmailStr you have to do this
pip install email-validator
satya - 3/3/2024, 6:16:12 PM
Here is how to use annotated validators
satya - 3/3/2024, 8:35:57 PM
Here are various ways to constrain fields including annotated
Here are various ways to constrain fields including annotated
satya - 3/4/2024, 11:36:45 AM
Field validators are documented here