Skip to content

Check for the "deprecated" keyword #1171

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions jsonschema/_keywords.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,3 +447,8 @@ def prefixItems(validator, prefixItems, instance, schema):
schema_path=index,
path=index,
)


def deprecated(validator, deprecated, instance, schema):
if validator.check_deprecated and deprecated is True:
yield ValidationError(f"{instance!r} is deprecated")
12 changes: 9 additions & 3 deletions jsonschema/protocols.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ class Validator(Protocol):
its `extra (optional) dependencies <index:extras>` when
invoking ``pip``.

check_deprecated:

if ``True``, raise validation errors when the :kw:`deprecated`
keyword is ``true`` for an instance value, indicating it should not
be used and may be removed in the future.

.. deprecated:: v4.12.0

Subclassing validator classes now explicitly warns this is not part of
Expand Down Expand Up @@ -216,7 +222,7 @@ def evolve(self, **kwargs) -> Validator:

>>> validator = Draft202012Validator({})
>>> validator.evolve(schema={"type": "number"})
Draft202012Validator(schema={'type': 'number'}, format_checker=None)
Draft202012Validator(schema={'type': 'number'}, format_checker=None, check_deprecated=False)

The returned object satisfies the validator protocol, but may not
be of the same concrete class! In particular this occurs
Expand All @@ -226,5 +232,5 @@ def evolve(self, **kwargs) -> Validator:
>>> validator.evolve(
... schema={"$schema": Draft7Validator.META_SCHEMA["$id"]}
... )
Draft7Validator(schema=..., format_checker=None)
"""
Draft7Validator(schema=..., format_checker=None, check_deprecated=False)
""" # noqa: E501
34 changes: 31 additions & 3 deletions jsonschema/tests/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ def test_repr(self):
self.addCleanup(validators._VALIDATORS.pop, "my version")
self.assertEqual(
repr(Validator({})),
"MyVersionValidator(schema={}, format_checker=None)",
"MyVersionValidator(schema={}, format_checker=None, "
"check_deprecated=False)",
)

def test_long_repr(self):
Expand All @@ -140,15 +141,16 @@ def test_long_repr(self):
self.assertEqual(
repr(Validator({"a": list(range(1000))})), (
"MyVersionValidator(schema={'a': [0, 1, 2, 3, 4, 5, ...]}, "
"format_checker=None)"
"format_checker=None, check_deprecated=False)"
),
)

def test_repr_no_version(self):
Validator = validators.create(meta_schema={})
self.assertEqual(
repr(Validator({})),
"Validator(schema={}, format_checker=None)",
"Validator(schema={}, format_checker=None, "
"check_deprecated=False)",
)

def test_dashes_are_stripped_from_validator_names(self):
Expand Down Expand Up @@ -530,6 +532,32 @@ def test_maxLength(self):
)
self.assertEqual(message, "'abc' is too long")

def test_deprecated(self):
message = self.message_for(
instance={"id": 1, "name": "abc"},
schema={
"properties": {
"id": {"type": "integer"},
"name": {"type": "string", "deprecated": True},
},
},
check_deprecated=True,
)
self.assertEqual(message, "'abc' is deprecated")

def test_deprecated_disabled(self):
schema = {
"properties": {
"id": {"type": "integer"},
"name": {"type": "string", "deprecated": True},
},
}
instance={"id": 1, "name": "abc"}

validator = validators.Draft202012Validator(schema)
errors = list(validator.iter_errors(instance))
self.assertListEqual(errors, [])

def test_pattern(self):
message = self.message_for(
instance="bbb",
Expand Down
3 changes: 3 additions & 0 deletions jsonschema/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ class Validator:
schema: referencing.jsonschema.Schema = field(repr=reprlib.repr)
_ref_resolver = field(default=None, repr=False, alias="resolver")
format_checker: _format.FormatChecker | None = field(default=None)
check_deprecated: bool = field(default=False)
# TODO: include new meta-schemas added at runtime
_registry: referencing.jsonschema.SchemaRegistry = field(
default=_REMOTE_WARNING_REGISTRY,
Expand Down Expand Up @@ -758,6 +759,7 @@ def extend(
"contains": _keywords.contains,
"dependentRequired": _keywords.dependentRequired,
"dependentSchemas": _keywords.dependentSchemas,
"deprecated": _keywords.deprecated,
"enum": _keywords.enum,
"exclusiveMaximum": _keywords.exclusiveMaximum,
"exclusiveMinimum": _keywords.exclusiveMinimum,
Expand Down Expand Up @@ -805,6 +807,7 @@ def extend(
"contains": _keywords.contains,
"dependentRequired": _keywords.dependentRequired,
"dependentSchemas": _keywords.dependentSchemas,
"deprecated": _keywords.deprecated,
"enum": _keywords.enum,
"exclusiveMaximum": _keywords.exclusiveMaximum,
"exclusiveMinimum": _keywords.exclusiveMinimum,
Expand Down