Skip to content

Commit 4369a15

Browse files
committed
Merge remote-tracking branch 'TiborVoelcker/automatic_format_checker'
* TiborVoelcker/automatic_format_checker: [pre-commit.ci] auto fixes from pre-commit.com hooks Add FORMAT_CHECKER Use standard .gitignore
2 parents 6787b21 + 36291ae commit 4369a15

File tree

3 files changed

+187
-7
lines changed

3 files changed

+187
-7
lines changed

.gitignore

Lines changed: 154 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,157 @@
1-
_cache
2-
_static
3-
_templates
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
45

6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
share/python-wheels/
524
*.egg-info/
6-
build
7-
dist
25+
.installed.cfg
26+
*.egg
27+
MANIFEST
28+
29+
# PyInstaller
30+
# Usually these files are written by a python script from a template
31+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
32+
*.manifest
33+
*.spec
34+
35+
# Installer logs
36+
pip-log.txt
37+
pip-delete-this-directory.txt
38+
39+
# Unit test / coverage reports
40+
htmlcov/
41+
.tox/
42+
.nox/
43+
.coverage
44+
.coverage.*
45+
.cache
46+
nosetests.xml
47+
coverage.xml
48+
*.cover
49+
*.py,cover
50+
.hypothesis/
51+
.pytest_cache/
52+
cover/
53+
54+
# Translations
55+
*.mo
56+
*.pot
57+
58+
# Django stuff:
59+
*.log
60+
local_settings.py
61+
db.sqlite3
62+
db.sqlite3-journal
63+
64+
# Flask stuff:
65+
instance/
66+
.webassets-cache
67+
68+
# Scrapy stuff:
69+
.scrapy
70+
71+
# Sphinx documentation
72+
docs/_build/
73+
74+
# PyBuilder
75+
.pybuilder/
76+
target/
77+
78+
# Jupyter Notebook
79+
.ipynb_checkpoints
80+
81+
# IPython
82+
profile_default/
83+
ipython_config.py
84+
85+
# pyenv
86+
# For a library or package, you might want to ignore these files since the code is
87+
# intended to run in multiple environments; otherwise, check them in:
88+
# .python-version
89+
90+
# pipenv
91+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
93+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
94+
# install all needed dependencies.
95+
#Pipfile.lock
896

9-
TODO
97+
# poetry
98+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
99+
# This is especially recommended for binary packages to ensure reproducibility, and is more
100+
# commonly ignored for libraries.
101+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
102+
#poetry.lock
103+
104+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
105+
__pypackages__/
106+
107+
# Celery stuff
108+
celerybeat-schedule
109+
celerybeat.pid
110+
111+
# SageMath parsed files
112+
*.sage.py
113+
114+
# Environments
115+
.env
116+
.venv
117+
env/
118+
venv/
119+
ENV/
120+
env.bak/
121+
venv.bak/
122+
123+
# Spyder project settings
124+
.spyderproject
125+
.spyproject
126+
127+
# Rope project settings
128+
.ropeproject
129+
130+
# mkdocs documentation
131+
/site
132+
133+
# mypy
134+
.mypy_cache/
135+
.dmypy.json
136+
dmypy.json
137+
138+
# Pyre type checker
139+
.pyre/
140+
141+
# pytype static type analyzer
142+
.pytype/
143+
144+
# Cython debug symbols
145+
cython_debug/
146+
147+
# PyCharm
148+
# JetBrains specific template is maintainted in a separate JetBrains.gitignore that can
149+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
150+
# and can be added to the global gitignore or merged into this file. For a more nuclear
151+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
152+
#.idea/
153+
154+
# User defined
155+
_cache
156+
_static
157+
_templates

jsonschema/protocols.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ class Validator(Protocol):
7979
#: :validator:`type` properties in JSON schemas.
8080
TYPE_CHECKER: ClassVar[jsonschema.TypeChecker]
8181

82+
#: A `jsonschema.FormatChecker` that will be used when validating
83+
#: :validator:`format` properties in JSON schemas.
84+
FORMAT_CHECKER: ClassVar[jsonschema.FormatChecker]
85+
8286
#: The schema that was passed in when initializing the object.
8387
schema: dict | bool
8488

jsonschema/validators.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import attr
1818

1919
from jsonschema import (
20+
_format,
2021
_legacy_validators,
2122
_types,
2223
_utils,
@@ -108,6 +109,7 @@ def create(
108109
validators=(),
109110
version=None,
110111
type_checker=_types.draft7_type_checker,
112+
format_checker=_format.draft7_format_checker,
111113
id_of=_id_of,
112114
applicable_validators=lambda schema: schema.items(),
113115
):
@@ -148,6 +150,13 @@ def create(
148150
If unprovided, a `jsonschema.TypeChecker` will be created
149151
with a set of default types typical of JSON Schema drafts.
150152
153+
format_checker (jsonschema.FormatChecker):
154+
155+
a format checker, used when applying the :validator:`format` validator.
156+
157+
If unprovided, a `jsonschema.FormatChecker` will be created
158+
with a set of default formats typical of JSON Schema drafts.
159+
151160
id_of (collections.abc.Callable):
152161
153162
A function that given a schema, returns its ID.
@@ -162,13 +171,16 @@ def create(
162171
163172
a new `jsonschema.protocols.Validator` class
164173
"""
174+
# rename to not clash with "format_checker" argument of `Validator.__init__()`
175+
fmt_checker = format_checker
165176

166177
@attr.s
167178
class Validator:
168179

169180
VALIDATORS = dict(validators)
170181
META_SCHEMA = dict(meta_schema)
171182
TYPE_CHECKER = type_checker
183+
FORMAT_CHECKER = fmt_checker
172184
ID_OF = staticmethod(id_of)
173185

174186
schema = attr.ib(repr=reprlib.repr)
@@ -283,7 +295,7 @@ def is_valid(self, instance, _schema=None):
283295
return Validator
284296

285297

286-
def extend(validator, validators=(), version=None, type_checker=None):
298+
def extend(validator, validators=(), version=None, type_checker=None, format_checker=None):
287299
"""
288300
Create a new validator class by extending an existing one.
289301
@@ -321,6 +333,13 @@ def extend(validator, validators=(), version=None, type_checker=None):
321333
If unprovided, the type checker of the extended
322334
`jsonschema.protocols.Validator` will be carried along.
323335
336+
format_checker (jsonschema.FormatChecker):
337+
338+
a format checker, used when applying the :validator:`format` validator.
339+
340+
If unprovided, the format checker of the extended
341+
`jsonschema.protocols.Validator` will be carried along.
342+
324343
Returns:
325344
326345
a new `jsonschema.protocols.Validator` class extending the one
@@ -342,11 +361,14 @@ def extend(validator, validators=(), version=None, type_checker=None):
342361

343362
if type_checker is None:
344363
type_checker = validator.TYPE_CHECKER
364+
if format_checker is None:
365+
format_checker = validator.FORMAT_CHECKER
345366
return create(
346367
meta_schema=validator.META_SCHEMA,
347368
validators=all_validators,
348369
version=version,
349370
type_checker=type_checker,
371+
format_checker=format_checker,
350372
id_of=validator.ID_OF,
351373
)
352374

@@ -377,6 +399,7 @@ def extend(validator, validators=(), version=None, type_checker=None):
377399
"uniqueItems": _validators.uniqueItems,
378400
},
379401
type_checker=_types.draft3_type_checker,
402+
format_checker=_format.draft3_format_checker,
380403
version="draft3",
381404
id_of=lambda schema: schema.get("id", ""),
382405
applicable_validators=_legacy_validators.ignore_ref_siblings,
@@ -413,6 +436,7 @@ def extend(validator, validators=(), version=None, type_checker=None):
413436
"uniqueItems": _validators.uniqueItems,
414437
},
415438
type_checker=_types.draft4_type_checker,
439+
format_checker=_format.draft4_format_checker,
416440
version="draft4",
417441
id_of=lambda schema: schema.get("id", ""),
418442
applicable_validators=_legacy_validators.ignore_ref_siblings,
@@ -454,6 +478,7 @@ def extend(validator, validators=(), version=None, type_checker=None):
454478
"uniqueItems": _validators.uniqueItems,
455479
},
456480
type_checker=_types.draft6_type_checker,
481+
format_checker=_format.draft6_format_checker,
457482
version="draft6",
458483
applicable_validators=_legacy_validators.ignore_ref_siblings,
459484
)
@@ -495,6 +520,7 @@ def extend(validator, validators=(), version=None, type_checker=None):
495520
"uniqueItems": _validators.uniqueItems,
496521
},
497522
type_checker=_types.draft7_type_checker,
523+
format_checker=_format.draft7_format_checker,
498524
version="draft7",
499525
applicable_validators=_legacy_validators.ignore_ref_siblings,
500526
)
@@ -540,6 +566,7 @@ def extend(validator, validators=(), version=None, type_checker=None):
540566
"uniqueItems": _validators.uniqueItems,
541567
},
542568
type_checker=_types.draft201909_type_checker,
569+
format_checker=_format.draft201909_format_checker,
543570
version="draft2019-09",
544571
)
545572

@@ -585,6 +612,7 @@ def extend(validator, validators=(), version=None, type_checker=None):
585612
"uniqueItems": _validators.uniqueItems,
586613
},
587614
type_checker=_types.draft202012_type_checker,
615+
format_checker=_format.draft202012_format_checker,
588616
version="draft2020-12",
589617
)
590618

0 commit comments

Comments
 (0)