Skip to content

Commit 51506b9

Browse files
committed
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
1 parent d47db26 commit 51506b9

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

json/tests/draft7/optional/format/hostname.json

+10
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,16 @@
112112
"description": "single label ending with digit",
113113
"data": "hostnam3",
114114
"valid": true
115+
},
116+
{
117+
"description": "empty string",
118+
"data": "",
119+
"valid": false
120+
},
121+
{
122+
"description": "single dot",
123+
"data": ".",
124+
"valid": false
115125
}
116126
]
117127
}

jsonschema/_format.py

+4
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,10 @@ def is_ipv6(instance: object) -> bool:
272272
draft7="hostname",
273273
draft201909="hostname",
274274
draft202012="hostname",
275+
# fqdn.FQDN("") raises a ValueError due to a bug
276+
# however, it's not clear when or if that will be fixed, so catch it
277+
# here for now
278+
raises=ValueError,
275279
)
276280
def is_host_name(instance: object) -> bool:
277281
if not isinstance(instance, str):

0 commit comments

Comments
 (0)