You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.6 KiB
49 lines
1.6 KiB
from pydantic import BaseModel, Field, validator |
|
from typing import List, Optional |
|
from datetime import datetime |
|
|
|
class PietanzaBase(BaseModel): |
|
nome: str = Field(..., min_length=1, max_length=150) |
|
descrizione: Optional[str] = None |
|
allergeni: Optional[List[str]] = [] |
|
|
|
class PietanzaCreate(PietanzaBase): |
|
@validator('allergeni') |
|
def validate_allergeni(cls, v): |
|
if v is None: |
|
return [] |
|
# Common allergens validation |
|
valid_allergeni = { |
|
'glutine', 'lattosio', 'uova', 'pesce', 'crostacei', 'arachidi', |
|
'frutta_a_guscio', 'soia', 'sedano', 'senape', 'sesamo', 'solfiti' |
|
} |
|
for allergen in v: |
|
if allergen not in valid_allergeni: |
|
raise ValueError(f"Allergene non valido: {allergen}") |
|
return v |
|
|
|
class PietanzaUpdate(BaseModel): |
|
nome: Optional[str] = Field(None, min_length=1, max_length=150) |
|
descrizione: Optional[str] = None |
|
allergeni: Optional[List[str]] = None |
|
|
|
@validator('allergeni') |
|
def validate_allergeni(cls, v): |
|
if v is None: |
|
return v |
|
valid_allergeni = { |
|
'glutine', 'lattosio', 'uova', 'pesce', 'crostacei', 'arachidi', |
|
'frutta_a_guscio', 'soia', 'sedano', 'senape', 'sesamo', 'solfiti' |
|
} |
|
for allergen in v: |
|
if allergen not in valid_allergeni: |
|
raise ValueError(f"Allergene non valido: {allergen}") |
|
return v |
|
|
|
class PietanzaResponse(PietanzaBase): |
|
id: int |
|
created_at: datetime |
|
updated_at: datetime |
|
|
|
class Config: |
|
from_attributes = True
|
|
|