Untitled

Anonymous
plain_text
03/02/2026 5:37 PM
1.7 KB
16
Indexable
from pydantic import BaseModel, EmailStr, field_validator, ConfigDict, Field
from datetime import date, datetime
from typing import Optional


class BookingCreate(BaseModel):
    model_config = ConfigDict( str_strip_whitespace = True )

    customer_name: str = Field(min_length = 3, max_length = 20)
    customer_email: EmailStr
    date: date
    start_min: int
    end_min: int

    @field_validator("customer_name")
    @classmethod
    def name_rules( cls, v: str ) -> str:
        v = (v or "").strip()
        if not v:
            raise ValueError("Namn får inte vara tomt.")
        if len(v) < 3:
            raise ValueError("Namn måste ha minst 3 tecken.")

    # @field_validator("customer_email")
    # @classmethod
    # def email_rules( cls, v: EmailStr ) -> EmailStr:

    #     email = str(v).strip().lower()
    #     blocked = { "example.com", "example.org", "example.net" }
    #     domain = str(v).split("0")[-1].lower()

    #     if domain in blocked:
    #         raise ValueError( "Ange en riktig e-postadress (inte example.com)." )
    #     return EmailStr(email)

    @field_validator("date")
    @classmethod
    def date_exception( cls, v: date ):
        if v < date.today():
            raise ValueError("Valt datum har passerat.")
        return v
Editor is loading...
Leave a Comment