Skip to content

Commit

Permalink
Deprecate returning False from validators
Browse files Browse the repository at this point in the history
  • Loading branch information
sloria committed Jan 6, 2025
1 parent b6357d0 commit 4b2c71c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Bug fixes:

Deprecations:

- Custom validators should raise a `ValidationError <marshmallow.exceptions.ValidationError>` for invalid values.
Returning `False`` is no longer supported .
- Deprecate ``context`` parameter of `Schema <marshmallow.schema.Schema>` (:issue:`1826`).
Use `contextVars.ContextVar` to pass context data instead.
- `Field <marshmallow.fields.Field>`, `Mapping <marshmallow.fields.Mapping>`,
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,6 @@ no_implicit_optional = true
[tool.pytest.ini_options]
norecursedirs = ".git .ropeproject .tox docs env venv tests/mypy_test_cases"
addopts = "-v --tb=short"
filterwarnings = [
"ignore:Returning `False` from a validator:marshmallow.warnings.ChangedInMarshmallow4Warning",
]
8 changes: 8 additions & 0 deletions src/marshmallow/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

import re
import typing
import warnings
from abc import ABC, abstractmethod
from itertools import zip_longest
from operator import attrgetter

from marshmallow import types
from marshmallow.exceptions import ValidationError
from marshmallow.warnings import ChangedInMarshmallow4Warning

_T = typing.TypeVar("_T")

Expand Down Expand Up @@ -77,6 +79,12 @@ def __call__(self, value: typing.Any) -> typing.Any:
try:
r = validator(value)
if not isinstance(validator, Validator) and r is False:
warnings.warn(
"Returning `False` from a validator is deprecated. "
"Raise a `ValidationError` instead.",
ChangedInMarshmallow4Warning,
stacklevel=2,
)
raise ValidationError(self.error)
except ValidationError as err:
kwargs.update(err.kwargs)
Expand Down

0 comments on commit 4b2c71c

Please sign in to comment.