Skip to content

Add PasswordStr type for password strength validation (follow-up to #1) #402

Description

@Fawas-Anayat

Context

In #1, a password type was listed as a candidate for this repo, with a
suggestion to use strip_whitespace + min_length as a baseline, and
optionally integrate with zxcvbn-python for realistic strength scoring.
That was flagged back in 2023 and doesn't seem to have been picked up
yet — I'd like to build it.

Problem

There's no reusable, configurable type for password validation in the
Pydantic ecosystem. Every project ends up hand-rolling the same regex
checks (length, character classes) for signup/auth forms. A shared type
here would save that repeated effort, the same way PhoneNumber or
CountryCode already do for their domains.

Proposed design

Two tiers, so simple cases stay simple and advanced cases get real
strength scoring:

Tier 1 — rule-based (no extra dependency, always available)

​```python
from pydantic_extra_types.password import PasswordStr
from pydantic import BaseModel
from typing import Annotated

class SignupForm(BaseModel):
password: Annotated[
str,
PasswordStr(min_length=10, require_digit=True, require_special=True)
]
​```

Tier 2 — strength-scored (optional extra, as suggested in #1)

​```python

pip install "pydantic-extra-types[zxcvbn]"

from pydantic_extra_types.password import PasswordStr

class SignupForm(BaseModel):
password: Annotated[
str,
PasswordStr(min_score=3) # zxcvbn score 0-4
]
​```

This follows the existing pattern in this repo (e.g. pendulum,
phonenumbers as opt-in extras) rather than adding zxcvbn as a hard
dependency.

Errors would be granular per rule (missing_uppercase, missing_digit,
too_weak, etc.) rather than one generic message, so they're directly
usable in API error responses.

What I'm asking for

Before I start implementing: does this design direction (rule-based
core + optional zxcvbn extra) match what was originally envisioned in
#1? Happy to adjust the API shape based on feedback before opening a PR.
I can own the implementation, tests, and docs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions