Pydantic

satya - 3/3/2024, 4:23:52 PM

Homepage and docs

Homepage and docs

satya - 3/3/2024, 4:25:02 PM

The BaseModel methods and properties

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

  1. constr: Defines a string with constraints, such as minimum and maximum length, regex pattern matching, etc.
  2. conint: Represents an integer with constraints, like a range specified by minimum (ge, gt) and maximum (le, lt) values.
  3. confloat: A floating-point number with constraints, including minimum, maximum, and whether to allow infinity or NaN values.
  4. conlist: A list with constraints on its elements' type and the list's minimum and maximum length.
  5. condict: A dictionary with constraints on key and value types, as well as minimum and maximum size.
  6. condecimal: A decimal type with constraints such as fixed places and maximum digits.
  7. EmailStr: A string representing an email address, validated against a standard email regex pattern.
  8. UrlStr: A string formatted as a URL, validated via regex to ensure it's a well-formed URL.
  9. Json: A string that must be valid JSON; Pydantic will parse and serialize the JSON automatically.
  10. SecretStr and SecretBytes: For storing secrets like passwords and tokens. The secret values are not dumped in logs or error messages, enhancing security.
  11. IPv4Address, IPv6Address, IPvAnyAddress: Represent IPv4, IPv6, or either type of IP address, validated to ensure they are correctly formatted.
  12. UUID1, UUID4, UUID3, UUID5: Represents UUIDs (Universally Unique Identifiers) of various versions, ensuring they are valid UUID strings.
  13. FilePath, DirectoryPath, Path: Types for file and directory paths, ensuring the paths exist and optionally that the path is a file or directory.
  14. PositiveInt, NegativeInt, PositiveFloat, NegativeFloat: Integers or floats constrained to be strictly positive or negative.
  15. 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

The types are somewhat documented here

satya - 3/3/2024, 6:01:04 PM

Network types are documented here

Network types are 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

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

Field validators are documented here

satya - 3/4/2024, 11:37:02 AM

Model validators are here

Model validators are here