From f62ee3ea6dcd8d0e89241fbd9db30c10a8a0254b Mon Sep 17 00:00:00 2001 From: Raphael Odini Date: Wed, 15 Nov 2023 14:58:21 +0100 Subject: [PATCH] Replace field validators with pydantic tooling --- app/schemas.py | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) diff --git a/app/schemas.py b/app/schemas.py index 8ff6eea0..1cd233af 100644 --- a/app/schemas.py +++ b/app/schemas.py @@ -1,4 +1,3 @@ -import re from datetime import date from datetime import datetime @@ -22,20 +21,13 @@ class UserBase(BaseModel): class PriceCreate(BaseModel): model_config = ConfigDict(from_attributes=True, arbitrary_types_allowed=True) - product_code: str = Field(min_length=1) + product_code: str = Field(min_length=1, pattern="^[0-9]+$") price: float currency: str | Currency - location_osm_id: int + location_osm_id: int = Field(gt=0) location_osm_type: PriceLocationOSMType date: date - @field_validator("product_code") - def product_code_must_be_only_numbers(cls, v): - regex = r"^[0-9]+$" - if not re.match(regex, v): - raise ValueError("must only contain numbers") - return v - @field_validator("currency") def currency_is_valid(cls, v): try: @@ -43,12 +35,6 @@ def currency_is_valid(cls, v): except ValueError: raise ValueError("not a valid currency code") - @field_validator("location_osm_id") - def location_osm_id_must_be_positive(cls, v): - if v <= 0: - raise ValueError("must be positive") - return v - @field_serializer("currency") def serialize_currency(self, currency: Currency, _info): if type(currency) is Currency: