Skip to content

hostname format checker might raise ValueError #1121

@jvtm

Description

@jvtm

At least on empty string format: hostname validator raises ValueError and not the expected ValidationError.

>>> 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)
>>> validator.validate("")
....
    for error in errors:
  File ".../jsonschema/_validators.py", line 238, in format
    validator.format_checker.check(instance, format)
  File ".../jsonschema/_format.py", line 135, in check
    result = func(instance)
             ^^^^^^^^^^^^^^
  File ".../jsonschema/_format.py", line 276, in is_host_name
    return FQDN(instance).is_valid
           ^^^^^^^^^^^^^^
  File ".../fqdn/__init__.py", line 44, in __init__
    raise ValueError("fqdn must be str")
ValueError: fqdn must be str

This also fails on iter_errors() usage.

This might be enough to fix it:

--- a/jsonschema/_format.py
+++ b/jsonschema/_format.py
@@ -270,6 +270,7 @@ with suppress(ImportError):
         draft7="hostname",
         draft201909="hostname",
         draft202012="hostname",
+        raises=ValueError,
     )
     def is_host_name(instance: object) -> bool:
         if not isinstance(instance, str):

Sorry, no time to properly test + send a PR at this point in time.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugSomething doesn't work the way it should.Needs Test UpstreamIssues that need to have a test added to https://github.com/json-schema-org/JSON-Schema-Test-Suite

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions