[Python] Pydantic Validation Issue with Union Models and extra = "forbid"

Discussão em 'Python' iniciado por Stack, Março 1, 2025.

  1. Stack

    Stack Membro Participativo

    I am working with Pydantic models in FastAPI, and I have a structure where RCSMessage can be one of several possible models. I am using Union to allow multiple message types and extra = "forbid" to prevent unknown properties. However, when I send a JSON with a property that does not exist in any model, Pydantic does not raise an error as expected.

    Pydantic Models


    from pydantic import BaseModel, Field
    from typing import Optional, Union
    from datetime import datetime

    class MessageContact(BaseModel):
    userContact: Optional[str] = Field(
    None, pattern=r"^\+\d{1,15}$"
    ) # MSISDN E.164 format
    chatId: Optional[str] = None # Example UUID

    class RCSMessageBase(BaseModel):
    msgId: Optional[str] = None
    status: Optional[str] = None
    trafficType: Optional[str] = None
    expiry: Optional[datetime] = None # ISO 8601 date-time format
    timestamp: Optional[datetime] = None # ISO 8601 date-time format

    class Config:
    extra = "forbid"

    class SuggestedResponse(BaseModel):
    suggestedResponse: Optional[dict] = None

    class Config:
    extra = "forbid"

    class SharedData(BaseModel):
    sharedData: Optional[dict] = None

    class Config:
    extra = "forbid"

    class IsTyping(BaseModel):
    isTyping: Optional[str] = Field(None, pattern="^(active|idle)$")

    class Config:
    extra = "forbid"

    RCSMessageType = Union[
    SuggestedResponse, SharedData, IsTyping
    ]

    RCSMessage = Union[RCSMessageBase, RCSMessageType]

    class RCSMessageWithContactInfo(BaseModel):
    RCSMessage: RCSMessage
    messageContact: MessageContact

    Issue


    When I send the following request:

    {
    "RCSMessage": {
    "isTypingNoExiste": "active"
    },
    "messageContact": {
    "userContact": "+14251234567"
    }
    }


    I expect Pydantic to raise an error because isTypingNoExiste is not a valid property in IsTyping. However, the validation does not fail, and the JSON is accepted.

    Question


    How can I make Pydantic reject any unknown properties within RCSMessage, ensuring that only the defined properties in the models within the Union are allowed?

    Continue reading...

Compartilhe esta Página