Skip to content

email(), hostname(), uuid(), and cron() leak AttributeError for truthy non-string input #601

Backlog

Description

@SonicHedghog

Several validators raise an uncaught AttributeError for truthy non-string input instead of following the normal True / ValidationError contract.

Repro

With validators 0.35.0:

>>> import validators
>>> validators.email(1)
Traceback (most recent call last):
    ...
AttributeError: 'int' object has no attribute 'count'

>>> validators.hostname(1)
Traceback (most recent call last):
    ...
AttributeError: 'int' object has no attribute 'count'

>>> validators.uuid(1)
Traceback (most recent call last):
    ...
AttributeError: 'int' object has no attribute 'replace'

>>> validators.cron(1)
Traceback (most recent call last):
    ...
AttributeError: 'int' object has no attribute 'strip'

Other wrong-typed values such as object() behave similarly. Falsy values are already handled because of the early if not value checks:

>>> validators.email(None)
ValidationError(func=email, args={'value': None})

Expected behavior

Invalid input should return ValidationError (or raise that validation error in r_ve mode), not leak an implementation-level AttributeError.

Why this matters

Validators are often applied to form, JSON, or otherwise untrusted data where the type is not guaranteed. A wrong type should be reported through the same validation-error path as any other invalid value, rather than crashing the caller.

I did not include the IP address validators here because their non-string behavior is already tracked separately in #578.

Possible resolution

Add targeted isinstance(value, str) guards in the affected validators before calling string methods, while preserving the existing special handling of UUID objects in uuid(). Targeted guards avoid hiding unrelated implementation bugs.

I would be happy to open a PR with these guards and regression tests.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions