diff --git a/app/schemas/contact.py b/app/schemas/contact.py index 7057bc5..ab2bd87 100644 --- a/app/schemas/contact.py +++ b/app/schemas/contact.py @@ -4,7 +4,7 @@ from __future__ import annotations from decimal import Decimal -from pydantic import BaseModel, Field +from pydantic import BaseModel, Field, model_validator # ── ContactPerson ── @@ -71,6 +71,15 @@ class ContactPersonResponse(BaseModel): class ContactCreate(BaseModel): type: str = Field("company", pattern="^(company|person)$") + + @model_validator(mode="after") + def _require_name_or_firstname(self): + """Ensure company has a name or person has a firstname.""" + if self.type == "company" and not self.name: + raise ValueError("Company contacts require a 'name' field") + if self.type == "person" and not self.firstname: + raise ValueError("Person contacts require a 'firstname' field") + return self status: str | None = Field(None, pattern="^(lead|qualified|customer|inactive)$") name: str | None = Field(None, max_length=255) firstname: str | None = Field(None, max_length=100)