Skip to content

Schema

Schemas for user app.

UserSchema (UserSchema) pydantic-model

User schema.

Source code in users/schema.py
class UserSchema(ModelSchema):
    """User schema."""

    username: str

    email: EmailStr

    @validator("email")
    def validate_email(cls: "UserSchema", value: str) -> str:  # noqa
        """Validate email."""
        if CustomUser.objects.filter(email=value).exists():
            msg = "Email already exists"
            raise ValueError(msg)

        local_part, domain = value.split("@")

        validate_confusables_email(
            local_part=local_part,
            domain=domain,
            exception_class=ValueError,
        )

        validate_reserved_name(value=local_part, exception_class=ValueError)

        validate_reserved_name(value=domain, exception_class=ValueError)
        return value

    @validator("username")
    def validate_username(cls: "UserSchema", value: str) -> str:  # noqa
        """Validate username."""
        if CustomUser.objects.filter(username=value).exists():
            msg = "Username already exists"
            raise ValueError(msg)

        validate_confusables(value=value, exception_class=ValueError)

        validate_reserved_name(value=value, exception_class=ValueError)

        return value

    class Config:
        """User schema config."""

        model = CustomUser
        model_fields = ["username", "email", "full_name", "password"]

Last update: 2023-06-09
Authors: Mohamed-Kaizen