python-jsonschema / jsonschema

An implementation of the JSON Schema specification for Python
https://python-jsonschema.readthedocs.io
MIT License
4.61k stars 581 forks source link

fix: hostname format check on empty string raises ValueError #1127

Closed jvtm closed 1 year ago

jvtm commented 1 year ago

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 #1121.


:books: Documentation preview :books:: https://python-jsonschema--1127.org.readthedocs.build/en/1127/

jvtm commented 1 year ago

I don't have time to work on this right now. Pending on discussion and decisions on JSON-Schema-Test-Suite.

Regardless of what the tests check, maybe this should be included in the simple original form: add raises=ValueError.

jvtm commented 1 year ago

Changed from Draft to Ready, as this fixes the crash. Leaving JSON-Schema-Test-Suite modifications for people more familiar with that project and the actual (meta) schema related work.

Julian commented 1 year ago

All fine. I'm going to close this then as I wouldn't merge it without tests, but you or anyone is welcome to pick it up again when the upstream PR in the test suite is merged. Thanks for the efforts.