Last active
November 1, 2020 20:49
-
-
Save MartinThoma/67a65a3fa992b5b6239c523a678d3fda to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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