Skip to content
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

@model_validator not parsed as a Validators #4

Open
butterlyn opened this issue Jun 25, 2024 · 0 comments
Open

@model_validator not parsed as a Validators #4

butterlyn opened this issue Jun 25, 2024 · 0 comments
Assignees
Labels
feature New feature or request

Comments

@butterlyn
Copy link

Scenario:
Adding @model_validator methods to a Pydantic model.

Example:

from typing import Any, Self
from pydantic import model_validator, ConfigDict, BaseModel, Field


class ExampleModel(BaseModel):
    """An example model."""

    field_without_default: str
    """Shows the *[Required]* marker in the signature."""

    field_plain_with_validator: int = 100
    """Show standard field with type annotation."""

    @model_validator(mode="before")
    @classmethod
    def lowercase_only(cls, data: dict[str, Any]) -> dict[str, Any]:
        if isinstance(data.get("field_without_default"), str):
            data["field_without_default"] = data["field_without_default"].lower()
        return data

    @model_validator(mode="after")
    def at_least_one_truthy_field(self) -> Self:
        """At least one field should be truthy. """
        assert (self.field_without_default or self.field_plain_with_validator)
        return self

Expected behaviour:

  • @model_validator methods listed under heading "Validators" (similar to how @field_validtor methods are rendered)
  • @model_validator methods labeled as pydantic-validator or similar (similar to how @field_validator methods are labelled)

Actual behaviour:
Methods appear in MkDocs as a regular class method or instance method. Not listed under "Validators"

@pawamoy pawamoy added the feature New feature or request label Jun 30, 2024
@pawamoy pawamoy self-assigned this Jun 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants