Skip to content

Commit cbe7755

Browse files
authored
Fix resolution of format checker classes (#232)
Use `validator_for` on a dummy schema document to get the relevant validator class. This removes maintenance of a dialect table from check-jsonschema and fixes some of the dialects being declared incorrectly.
1 parent eed1aea commit cbe7755

File tree

2 files changed

+8
-19
lines changed

2 files changed

+8
-19
lines changed

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Unreleased
1515
The metaschema's schema dialect is chosen correctly now, and metaschema
1616
formats are now checked by default. This can be disabled with
1717
`--disable-format`.
18+
- Fix the resolution of `$schema` dialect to format checker classes
1819

1920
0.20.0
2021
------

src/check_jsonschema/formats.py

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,7 @@
66
import typing as t
77

88
import jsonschema
9-
10-
CHECKERS_BY_DIALECT = {
11-
"http://json-schema.org/draft-03/schema#": jsonschema.Draft3Validator.FORMAT_CHECKER,
12-
"http://json-schema.org/draft-04/schema#": jsonschema.Draft4Validator.FORMAT_CHECKER,
13-
"http://json-schema.org/draft-06/schema#": jsonschema.Draft6Validator.FORMAT_CHECKER,
14-
"http://json-schema.org/draft-07/schema#": jsonschema.Draft7Validator.FORMAT_CHECKER,
15-
"http://json-schema.org/draft-2019-09/schema": (
16-
jsonschema.Draft201909Validator.FORMAT_CHECKER
17-
),
18-
"http://json-schema.org/draft-2020-12/schema": (
19-
jsonschema.Draft202012Validator.FORMAT_CHECKER
20-
),
21-
}
22-
LATEST_DIALECT = "http://json-schema.org/draft-2020-12/schema"
9+
import jsonschema.validators
2310

2411

2512
def _regex_check(instance: t.Any) -> bool:
@@ -56,12 +43,13 @@ def __init__(
5643

5744

5845
def get_base_format_checker(schema_dialect: str | None) -> jsonschema.FormatChecker:
59-
# map a schema's `$schema` attribute (the dialect / JSON Schema version) to a matching
60-
# format checker
46+
# resolve the dialect, if given, to a validator class
6147
# default to the latest draft
62-
if schema_dialect is None or schema_dialect not in CHECKERS_BY_DIALECT:
63-
return CHECKERS_BY_DIALECT[LATEST_DIALECT]
64-
return CHECKERS_BY_DIALECT[schema_dialect]
48+
validator_class = jsonschema.validators.validator_for(
49+
{} if schema_dialect is None else {"$schema": schema_dialect},
50+
default=jsonschema.Draft202012Validator,
51+
)
52+
return validator_class.FORMAT_CHECKER
6553

6654

6755
def make_format_checker(

0 commit comments

Comments
 (0)