Skip to content

Skip Validation of a Field Based on the Contents of Another #9208

Answered by uriyyo
iambroadband asked this question in Question
Discussion options

You must be logged in to vote

It's should be possible to achive it using wrap validator:

from enum import StrEnum, auto
from typing import Any, Callable

from pydantic import BaseModel, Field, field_validator
from pydantic_core.core_schema import ValidationInfo


class Color(StrEnum):
    BLUE = "U"
    RED = "R"


class BorderColor(StrEnum):
    BLACK = auto()
    BORDERLESS = auto()
    SILVER = auto()


class MyModel(BaseModel):
    border_color: BorderColor | None = Field(
        default=None,
        description="Border color of this card.",
    )
    produced_colors: set[Color] | None = Field(
        default=None,
        description="Colors this card can produce.",
    )

    @field_validator("produced_colors", 

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@iambroadband
Comment options

Answer selected by iambroadband
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants