Skip to content

Instantly share code, notes, and snippets.

@MartinThoma
Last active November 1, 2020 20:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MartinThoma/67a65a3fa992b5b6239c523a678d3fda to your computer and use it in GitHub Desktop.
Save MartinThoma/67a65a3fa992b5b6239c523a678d3fda to your computer and use it in GitHub Desktop.
from decimal import Decimal
from typing import List, NewType, Optional
import datetime
from pydantic import BaseModel, root_validator
PersonId = NewType("PersonId", int)
class Person(BaseModel):
id: PersonId
name: str
bank_account: Decimal
birthdate: datetime.date
friends: Optional[List[PersonId]] = None
class Config:
extra = "forbid"
@root_validator
def cannot_self_friend(cls, values):
friends = values.get("friends")
self_id = values.get("id")
if friends is not None and any(friend_id == self_id for friend_id in friends):
raise ValueError("You cannot be a friend of yourself")
return values
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment