|
6 | 6 | import typing as t
|
7 | 7 |
|
8 | 8 | 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 |
23 | 10 |
|
24 | 11 |
|
25 | 12 | def _regex_check(instance: t.Any) -> bool:
|
@@ -56,12 +43,13 @@ def __init__(
|
56 | 43 |
|
57 | 44 |
|
58 | 45 | 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 |
61 | 47 | # 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 |
65 | 53 |
|
66 | 54 |
|
67 | 55 | def make_format_checker(
|
|
0 commit comments