Skip to content

How to use root_validator in Pydantic v2? API docs miss a link #7975

@icfly2

Description

@icfly2

Discussed in #7060

Originally posted by deivydaslipskis August 9, 2023
We are migrating from Pydantic v1 to Pydantic v2 and there are issues with mypy and root validator.

Here is the error received:

Mypy:
my_proj/settings.py:191: error: No overload variant of "root_validator" matches argument type "Callable[[Settings, Any], Any]"  [call-overload]
my_proj/settings.py:191: note: Possible overload variants:
my_proj/settings.py:191: note:     def root_validator(*, skip_on_failure: Literal[True], allow_reuse: bool = ...) -> Callable[[_V1RootValidatorFunctionType], _V1RootValidatorFunctionType]
my_proj/settings.py:191: note:     def root_validator(*, pre: Literal[True], allow_reuse: bool = ...) -> Callable[[_V1RootValidatorFunctionType], _V1RootValidatorFunctionType]
my_proj/settings.py:191: note:     def root_validator(*, pre: Literal[False], skip_on_failure: Literal[True], allow_reuse: bool = ...) -> Callable[[_V1RootValidatorFunctionType], _V1RootValidatorFunctionType]

Here is an example of how we used root_validator in Pydantic v1:

from pydantic import root_validator

    @root_validator
    def validate_input(cls, values: Any) -> Any:
        if values.get("file_input_path") and values.get("redis_key"):
            raise ValueError("Either file_input or redis_input should be used!")
        return values

I also could not find any documentation on root_validator in Pydantic v2 version, is it deprecated and should I use something else? If so, what should I use?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions