diff --git a/app/schemas/contact.py b/app/schemas/contact.py index ab2bd87..852557b 100644 --- a/app/schemas/contact.py +++ b/app/schemas/contact.py @@ -75,6 +75,13 @@ class ContactCreate(BaseModel): @model_validator(mode="after") def _require_name_or_firstname(self): """Ensure company has a name or person has a firstname.""" + # Infer type from payload when the caller did not send it explicitly: + # person-like fields (firstname/surname) -> person, otherwise company. + if "type" not in self.model_fields_set: + if self.firstname or self.surname: + self.type = "person" + elif self.name: + self.type = "company" if self.type == "company" and not self.name: raise ValueError("Company contacts require a 'name' field") if self.type == "person" and not self.firstname: