Skip to content

Commit

Permalink
Improve 'format:hostname' to handle "" and "."
Browse files Browse the repository at this point in the history
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
  • Loading branch information
sirosen committed Jan 17, 2025
1 parent d47db26 commit 51506b9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
10 changes: 10 additions & 0 deletions json/tests/draft7/optional/format/hostname.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
]
}
Expand Down
4 changes: 4 additions & 0 deletions jsonschema/_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 51506b9

Please sign in to comment.