Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Conversation

jvtm
Copy link
Contributor

@jvtm jvtm commented Jul 13, 2023

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.


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

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.
@jvtm jvtm temporarily deployed to PyPI July 13, 2023 09:43 — with GitHub Actions Inactive
@jvtm jvtm marked this pull request as draft July 13, 2023 11:16
@jvtm
Copy link
Contributor Author

jvtm commented Jul 18, 2023

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 jvtm marked this pull request as ready for review July 18, 2023 11:43
@jvtm
Copy link
Contributor Author

jvtm commented Jul 18, 2023

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
Copy link
Member

Julian commented Jul 18, 2023

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.

@Julian Julian closed this Jul 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

hostname format checker might raise ValueError
2 participants