All contributions

pydantic/pydantic-extra-types

pydantic-extra-types

Extra types for data validation in Pydantic, the popular Python library: MAC addresses, time zones, semantic versions, ULIDs and many more.

Language
Python
GitHub stars
345
Merged changes
2

What we changed

A new Sha256Str type for SHA-256 digests, also visible in the JSON schema, and MacAddress no longer accepts octets with a plus or minus sign or whitespace.

What this work shows

  • Pydantic v2 data validation (core schema)
  • JSON Schema generation
  • Number parsing pitfalls in Python
  • Parametrised tests in pytest
What was broken

MacAddress accepted octets with a sign or space

validate_mac_address converted each octet with int(..., 16), which accepts a plus or minus sign and whitespace. The address -a:-b:-c:-d:-e:-f passed unchanged, and +a:+b:+c:+d:+e:+f was silently turned into 0a:0b:0c:0d:0e:0f, an address nobody provided.

How we fixed it

An octet is exactly two hex digits

Before conversion both characters of an octet must be in string.hexdigits. Otherwise validation raises the same mac_address_format error as for other malformed octets, and new test cases cover the colon and dot formats.

#424

What was broken

No type for a SHA-256 digest

Pydantic has a Base64Str type, but a SHA-256 digest, such as a file checksum, had to be checked with a hand-written validator. The issue asking for such a type pointed to Base64Str as the model.

How we fixed it

Sha256Str: 64 hex characters, lowercased

The new Sha256Str type strips whitespace, lowercases the letters and requires exactly 64 hexadecimal characters. The rule is a str_schema pattern, so it also shows up in the JSON schema, and tests reject a 0x prefix and non-ASCII digits among others.

#433

Merged changes 2

  1. #433 Add Sha256Str type Merged 3 October 2026
  2. #424 Fix MacAddress accepting octets with a sign or whitespace Merged 5 September 2026

What it taught us

A validation gap can come from a standard function's behaviour, here int() with base 16, and only a test closes it. We build new types so the same rule applies during validation and in the generated JSON schema.

More about this technology: Python

Other projects we write code for

Describe your project.

A free quote comes back within an hour during working hours.