Skip to content

Commit 07665ec

Browse files
authored
Merge pull request #719 from willson-chen/fix_hostname_bug
fix bug about hostname by import fqdn
2 parents 506cea3 + 95f38a6 commit 07665ec

File tree

6 files changed

+110
-67
lines changed

6 files changed

+110
-67
lines changed

.github/workflows/ci.yml

Lines changed: 84 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,17 @@ jobs:
1818
os: [macos-latest, ubuntu-latest, windows-latest]
1919
python-version:
2020
- name: pypy3
21-
toxenv: pypy3-build
21+
toxenv: pypy3-noextra-build
2222
- name: pypy3
23-
toxenv: pypy3-tests
23+
toxenv: pypy3-noextra-tests
2424
- name: pypy3
25-
toxenv: pypy3-tests_nongpl
25+
toxenv: pypy3-format-build
26+
- name: pypy3
27+
toxenv: pypy3-format-tests
28+
- name: pypy3
29+
toxenv: pypy3-format_nongpl-build
30+
- name: pypy3
31+
toxenv: pypy3-format_nongpl-tests
2632
- name: pypy3
2733
toxenv: demo
2834
- name: pypy3
@@ -44,36 +50,66 @@ jobs:
4450
- name: pypy3
4551
toxenv: docs-style
4652
- name: 3.6
47-
toxenv: py36-build
53+
toxenv: py36-noextra-build
54+
- name: 3.6
55+
toxenv: py36-noextra-tests
56+
- name: 3.6
57+
toxenv: py36-format-build
58+
- name: 3.6
59+
toxenv: py36-format-tests
4860
- name: 3.6
49-
toxenv: py36-tests
61+
toxenv: py36-format_nongpl-build
5062
- name: 3.6
51-
toxenv: py36-tests_nongpl
63+
toxenv: py36-format_nongpl-tests
5264
- name: 3.7
53-
toxenv: py37-build
65+
toxenv: py37-noextra-build
5466
- name: 3.7
55-
toxenv: py37-tests
67+
toxenv: py37-noextra-tests
5668
- name: 3.7
57-
toxenv: py37-tests_nongpl
69+
toxenv: py37-format-build
70+
- name: 3.7
71+
toxenv: py37-format-tests
72+
- name: 3.7
73+
toxenv: py37-format_nongpl-build
74+
- name: 3.7
75+
toxenv: py37-format_nongpl-tests
76+
- name: 3.8
77+
toxenv: py38-noextra-build
78+
- name: 3.8
79+
toxenv: py38-noextra-tests
5880
- name: 3.8
59-
toxenv: py38-build
81+
toxenv: py38-format-build
6082
- name: 3.8
61-
toxenv: py38-tests
83+
toxenv: py38-format-tests
6284
- name: 3.8
63-
toxenv: py38-tests_nongpl
85+
toxenv: py38-format_nongpl-build
86+
- name: 3.8
87+
toxenv: py38-format_nongpl-tests
6488
exclude:
6589
- os: windows-latest
6690
python-version:
6791
name: pypy3
68-
toxenv: pypy3-build
92+
toxenv: pypy3-noextra-build
93+
- os: windows-latest
94+
python-version:
95+
name: pypy3
96+
toxenv: pypy3-format-build
97+
- os: windows-latest
98+
python-version:
99+
name: pypy3
100+
toxenv: pypy3-format_nongpl-build
101+
- os: windows-latest
102+
python-version:
103+
name: pypy3
104+
toxenv: pypy3-noextra-tests
69105
- os: windows-latest
70106
python-version:
71107
name: pypy3
72-
toxenv: pypy3-tests
108+
toxenv: pypy3-format-tests
73109
- os: windows-latest
74110
python-version:
75111
name: pypy3
76-
toxenv: pypy3-tests_nongpl
112+
toxenv: pypy3-format_nongpl-tests
77113
- os: windows-latest
78114
python-version:
79115
name: pypy3
@@ -89,23 +125,51 @@ jobs:
89125
- os: windows-latest
90126
python-version:
91127
name: 3.6
92-
toxenv: py36-tests
128+
toxenv: py36-noextra-build
93129
- os: windows-latest
94130
python-version:
95131
name: 3.6
96-
toxenv: py36-tests_nongpl
132+
toxenv: py36-format-build
97133
- os: windows-latest
98134
python-version:
99135
name: 3.6
100-
toxenv: py36-build
136+
toxenv: py36-format_nongpl-build
137+
- os: windows-latest
138+
python-version:
139+
name: 3.6
140+
toxenv: py36-noextra-tests
141+
- os: windows-latest
142+
python-version:
143+
name: 3.6
144+
toxenv: py36-format-tests
145+
- os: windows-latest
146+
python-version:
147+
name: 3.6
148+
toxenv: py36-format_nongpl-tests
101149
- os: windows-latest
102150
python-version:
103151
name: 3.7
104-
toxenv: py37-build
152+
toxenv: py37-noextra-tests
153+
- os: windows-latest
154+
python-version:
155+
name: 3.7
156+
toxenv: py37-format-tests
157+
- os: windows-latest
158+
python-version:
159+
name: 3.7
160+
toxenv: py37-format_nongpl-tests
161+
- os: windows-latest
162+
python-version:
163+
name: 3.8
164+
toxenv: py38-noextra-tests
165+
- os: windows-latest
166+
python-version:
167+
name: 3.8
168+
toxenv: py38-format-tests
105169
- os: windows-latest
106170
python-version:
107171
name: 3.8
108-
toxenv: py38-build
172+
toxenv: py38-format_nongpl-tests
109173
- os: windows-latest
110174
python-version:
111175
name: pypy3

docs/validate.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,15 +277,15 @@ validation can be enabled by hooking in a format-checking object into an
277277

278278
.. doctest::
279279

280-
>>> validate("localhost", {"format" : "hostname"})
280+
>>> validate("127.0.0.1", {"format" : "ipv4"})
281281
>>> validate(
282282
... instance="-12",
283-
... schema={"format" : "hostname"},
283+
... schema={"format" : "ipv4"},
284284
... format_checker=draft7_format_checker,
285285
... )
286286
Traceback (most recent call last):
287287
...
288-
ValidationError: "-12" is not a "hostname"
288+
ValidationError: "-12" is not a "ipv4"
289289

290290
.. autoclass:: FormatChecker
291291
:members:

jsonschema/_format.py

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -209,25 +209,21 @@ def is_ipv6(instance):
209209
return socket.inet_pton(socket.AF_INET6, instance)
210210

211211

212-
_host_name_re = re.compile(r"^[A-Za-z0-9][A-Za-z0-9\.\-]{1,255}$")
213-
214-
215-
@_checks_drafts(
216-
draft3="host-name",
217-
draft4="hostname",
218-
draft6="hostname",
219-
draft7="hostname",
220-
)
221-
def is_host_name(instance):
222-
if not isinstance(instance, str):
223-
return True
224-
if not _host_name_re.match(instance):
225-
return False
226-
components = instance.split(".")
227-
for component in components:
228-
if len(component) > 63:
229-
return False
230-
return True
212+
try:
213+
from fqdn import FQDN
214+
except ImportError:
215+
pass
216+
else:
217+
@_checks_drafts(
218+
draft3="host-name",
219+
draft4="hostname",
220+
draft6="hostname",
221+
draft7="hostname",
222+
)
223+
def is_host_name(instance):
224+
if not isinstance(instance, str):
225+
return True
226+
return FQDN(instance).is_valid
231227

232228

233229
try:

jsonschema/tests/test_jsonschema_test_suite.py

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,6 @@ def narrow_unicode_build(test): # pragma: no cover
105105
subject="date-time",
106106
description="case-insensitive T and Z",
107107
)(test)
108-
or skip(
109-
message=bug(),
110-
subject="host-name",
111-
description="ends with hyphen",
112-
)(test)
113108
or skip(
114109
message=bug(686),
115110
subject="uniqueItems",
@@ -180,11 +175,6 @@ def narrow_unicode_build(test): # pragma: no cover
180175
subject="date-time",
181176
description="case-insensitive T and Z",
182177
)(test)
183-
or skip(
184-
message=bug(),
185-
subject="hostname",
186-
description="ends with hyphen",
187-
)(test)
188178
or skip(
189179
message=bug(686),
190180
subject="uniqueItems",
@@ -254,11 +244,6 @@ def narrow_unicode_build(test): # pragma: no cover
254244
subject="date-time",
255245
description="case-insensitive T and Z",
256246
)(test)
257-
or skip(
258-
message=bug(),
259-
subject="hostname",
260-
description="ends with hyphen",
261-
)(test)
262247
or skip(
263248
message=bug(686),
264249
subject="uniqueItems",
@@ -329,11 +314,6 @@ def narrow_unicode_build(test): # pragma: no cover
329314
subject="date-time",
330315
description="case-insensitive T and Z",
331316
)(test)
332-
or skip(
333-
message=bug(),
334-
subject="hostname",
335-
description="ends with hyphen",
336-
)(test)
337317
or skip(
338318
message=bug(593),
339319
subject="content",

setup.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,14 @@ install_requires =
3232

3333
[options.extras_require]
3434
format =
35+
fqdn
3536
idna
3637
jsonpointer>1.13
3738
rfc3987
3839
strict-rfc3339
3940
webcolors
4041
format_nongpl =
42+
fqdn
4143
idna
4244
jsonpointer>1.13
4345
webcolors

tox.ini

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tox]
22
envlist =
3-
py{36,37,38,py3}-{build,tests,tests_nongpl},
3+
py{36,37,38,py3}-{noextra,format,format_nongpl}-{build,tests},
44
demo
55
readme
66
safety
@@ -16,10 +16,11 @@ setenv =
1616
whitelist_externals =
1717
mkdir
1818
commands =
19-
perf,tests: {envpython} -m pip install '{toxinidir}[format]'
20-
tests_nongpl: {envpython} -m pip install '{toxinidir}[format_nongpl]'
19+
noextra: {envpython} -m pip install {toxinidir}
20+
format: {envpython} -m pip install '{toxinidir}[format]'
21+
format_nongpl: {envpython} -m pip install '{toxinidir}[format_nongpl]'
2122

22-
tests,tests_nongpl: {envpython} -m twisted.trial {posargs:jsonschema}
23+
tests: {envpython} -m twisted.trial {posargs:jsonschema}
2324
tests: {envpython} -m doctest {toxinidir}/README.rst
2425

2526
perf: mkdir {envtmpdir}/benchmarks/
@@ -33,7 +34,7 @@ deps =
3334

3435
perf: pyperf
3536

36-
tests,tests_nongpl,coverage,codecov: twisted
37+
tests,coverage,codecov: twisted
3738

3839
coverage,codecov: coverage
3940
codecov: codecov

0 commit comments

Comments
 (0)