From 51506b97291adac6e3ca926442eb07f787bc98b5 Mon Sep 17 00:00:00 2001 From: Stephen Rosen Date: Thu, 16 Jan 2025 22:14:04 -0600 Subject: [PATCH] Improve 'format:hostname' to handle "" and "." The `fqdn` library used to provide `"hostname"` format validation (when installed) does not properly handle some known inputs, including `""` and `"."`. On these strings, the library improperly emits `ValueError`. Given that the library no longer appears to be receiving updates, there are two potential workarounds in `jsonschema`: 1. add `ValueError` handling via `raises=...` 2. add explicit handling for known patterns before calling out to `fqdn` This changeset implements (1) only, adding handling for `ValueError`. New test cases are added for `""` and `"."`. resolves #1121 --- json/tests/draft7/optional/format/hostname.json | 10 ++++++++++ jsonschema/_format.py | 4 ++++ 2 files changed, 14 insertions(+) diff --git a/json/tests/draft7/optional/format/hostname.json b/json/tests/draft7/optional/format/hostname.json index a8ecd194..866a6178 100644 --- a/json/tests/draft7/optional/format/hostname.json +++ b/json/tests/draft7/optional/format/hostname.json @@ -112,6 +112,16 @@ "description": "single label ending with digit", "data": "hostnam3", "valid": true + }, + { + "description": "empty string", + "data": "", + "valid": false + }, + { + "description": "single dot", + "data": ".", + "valid": false } ] } diff --git a/jsonschema/_format.py b/jsonschema/_format.py index 789fe4f2..9b4e67b6 100644 --- a/jsonschema/_format.py +++ b/jsonschema/_format.py @@ -272,6 +272,10 @@ def is_ipv6(instance: object) -> bool: draft7="hostname", draft201909="hostname", draft202012="hostname", + # fqdn.FQDN("") raises a ValueError due to a bug + # however, it's not clear when or if that will be fixed, so catch it + # here for now + raises=ValueError, ) def is_host_name(instance: object) -> bool: if not isinstance(instance, str):