from typing import Optional, List
from pydantic import BaseModel, validator, ValidationError
from sqlmodel import SQLModel, Field


class User(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    username: str = Field(nullable=False, sa_column_kwargs={"unique": True})
    superuser: bool = False
    password: str


# Serializers

class ValidatePassword(BaseModel):
    @validator('confirm_password', allow_reuse=True, check_fields=False)
    def validate_password(cls, v, values, **kwargs):
        if v != values['password']:
            raise ValueError("Passwords don't match")
        return v


class UserIn(ValidatePassword):
    username: str
    superuser: bool = False
    password: str
    confirm_password: str


class UserPatch(ValidatePassword):
    superuser: Optional[bool]
    password: Optional[str]
    confirm_password: Optional[str]


class UserOut(BaseModel):
    username: str
    superuser: bool


UserOutList = List[UserOut]


pydantic.validator sqlmodel.Field
[((190, 227), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'primary_key': '(True)'}), '(default=None, primary_key=True)\n', (195, 227), False, 'from sqlmodel import SQLModel, Field\n'), ((248, 304), 'sqlmodel.Field', 'Field', ([], {'nullable': '(False)', 'sa_column_kwargs': "{'unique': True}"}), "(nullable=False, sa_column_kwargs={'unique': True})\n", (253, 304), False, 'from sqlmodel import SQLModel, Field\n'), ((408, 475), 'pydantic.validator', 'validator', (['"""confirm_password"""'], {'allow_reuse': '(True)', 'check_fields': '(False)'}), "('confirm_password', allow_reuse=True, check_fields=False)\n", (417, 475), False, 'from pydantic import BaseModel, validator, ValidationError\n')]