Skip to content

Commit

Permalink
fix: hostname format check on empty string raises ValueError
Browse files Browse the repository at this point in the history
Doing hostname format check on empty string seems to raise a ValueError:

    >>> from jsonschema.validators import validator_for
    >>> schema = {"$schema": "https://json-schema.org/draft/2020-12/schema", "type": "string", "format": "hostname"}
    >>> vcls = validator_for(schema)
    >>> validator = vcls(schema, format_checker=vcls.FORMAT_CHECKER)
    >>> list(validator.iter_errors(""))
    ...
      File "lib/python3.10/site-packages/jsonschema/_format.py", line 276, in is_host_name
        return FQDN(instance).is_valid
      File "lib/python3.10/site-packages/fqdn/__init__.py", line 44, in __init__
        raise ValueError("fqdn must be str")
    ValueError: fqdn must be str

Fix by adding `raises=ValueError` to the related `@_checks_drafts` decorator call.

See also json-schema-org/JSON-Schema-Test-Suite#677.

Fixes python-jsonschema#1121.
  • Loading branch information
jvtm committed Jul 13, 2023
1 parent 52c2419 commit ce59652
Showing 1 changed file with 1 addition and 0 deletions.
1 change: 1 addition & 0 deletions jsonschema/_format.py
Expand Up @@ -270,6 +270,7 @@ def is_ipv6(instance: object) -> bool:
draft7="hostname",
draft201909="hostname",
draft202012="hostname",
raises=ValueError,
)
def is_host_name(instance: object) -> bool:
if not isinstance(instance, str):
Expand Down

0 comments on commit ce59652

Please sign in to comment.