Skip to content

Commit 4fde3f6

Browse files
committed
менее строгая валидация
1 parent bd03df0 commit 4fde3f6

File tree

3 files changed

+61
-59
lines changed

3 files changed

+61
-59
lines changed

src/openinflation_dataclass/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Pydantic models for products, categories, and retail geolocation."""
22

3-
__version__ = "0.1.1"
3+
__version__ = "0.1.2"
44

55
from .card import Card, MetaData, WholesalePrice
66
from .geolocation import AdministrativeUnit, RetailUnit, Schedule

src/openinflation_dataclass/card.py

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,52 +10,52 @@
1010
class WholesalePrice(NetworkModel):
1111
"""Wholesale unit price threshold."""
1212

13-
from_items: int | float
14-
price: float
13+
from_items: int | float | None = None
14+
price: float | None = None
1515

1616

1717
class MetaData(NetworkModel):
1818
"""Additional product metadata."""
1919

20-
name: str
21-
alias: str
22-
value: float | int | str
20+
name: str | None = None
21+
alias: str | None = None
22+
value: float | int | str | None = None
2323

2424

2525
class Card(NetworkModel):
2626
"""Product card model from a source catalog."""
2727

28-
sku: str
29-
plu: str | None
30-
source_page_url: str
28+
sku: str | None = None
29+
plu: str | None = None
30+
source_page_url: str | None = None
3131

32-
title: str
33-
description: str
32+
title: str | None = None
33+
description: str | None = None
3434

35-
adult: bool
36-
new: bool
37-
promo: bool
38-
season: bool
39-
hit: bool
40-
data_matrix: bool
35+
adult: bool | None = None
36+
new: bool | None = None
37+
promo: bool | None = None
38+
season: bool | None = None
39+
hit: bool | None = None
40+
data_matrix: bool | None = None
4141

42-
brand: str
43-
producer_name: str
44-
producer_country: Literal["BLR", "RUS", "USA", "ARE", "CHN"]
42+
brand: str | None = None
43+
producer_name: str | None = None
44+
producer_country: Literal["BLR", "RUS", "USA", "ARE", "CHN"] | None = None
4545

46-
composition: str
47-
meta_data: list[MetaData]
46+
composition: str | None = None
47+
meta_data: list[MetaData] | None = Field(default_factory=list)
4848

49-
expiration_date_in_days: int
49+
expiration_date_in_days: int | None = None
5050

51-
rating: float
52-
reviews_count: int
51+
rating: float | None = None
52+
reviews_count: int | None = None
5353

54-
price: float
55-
discount_price: float | None
56-
loyal_price: float | None
57-
wholesale_price: list[WholesalePrice]
58-
price_unit: Literal["BYN", "RUB", "USD", "EUR", "AED"]
54+
price: float | None = None
55+
discount_price: float | None = None
56+
loyal_price: float | None = None
57+
wholesale_price: list[WholesalePrice] | None = Field(default_factory=list)
58+
price_unit: Literal["BYN", "RUB", "USD", "EUR", "AED"] | None = None
5959

6060
# Unit guide:
6161
# Chocolate 200 g:
@@ -66,15 +66,15 @@ class Card(NetworkModel):
6666
# unit="KGM", available_count=12.7, package_quantity=None, package_unit=None
6767
# Water vending:
6868
# unit="LTR", available_count=29.2, package_quantity=0.5, package_unit="LTR"
69-
unit: Literal["PCE", "KGM", "LTR"]
70-
available_count: int | float | None
71-
package_quantity: float | None
72-
package_unit: Literal["KGM", "LTR"] | None
69+
unit: Literal["PCE", "KGM", "LTR"] | None = None
70+
available_count: int | float | None = None
71+
package_quantity: float | None = None
72+
package_unit: Literal["KGM", "LTR"] | None = None
7373

74-
categories_uid: list[str]
74+
categories_uid: list[str] | None = Field(default_factory=list)
7575

76-
main_image: str = Field(repr=False)
77-
images: list[str] = Field(default_factory=list, repr=False)
76+
main_image: str | None = Field(default=None, repr=False)
77+
images: list[str] | None = Field(default_factory=list, repr=False)
7878

7979
@model_validator(mode="after")
8080
def validate_business_rules(self) -> Card:

src/openinflation_dataclass/geolocation.py

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ def _validate_hhmm(value: str, field_name: str) -> None:
1919
class Schedule(NetworkModel):
2020
"""Opening/closing time in HH:MM format."""
2121

22-
open_from: str
23-
closed_from: str
22+
open_from: str | None = None
23+
closed_from: str | None = None
2424

2525
@field_validator("open_from", "closed_from")
2626
@classmethod
27-
def validate_hhmm(cls, value: str, info: object) -> str:
27+
def validate_hhmm(cls, value: str | None, info: object) -> str | None:
28+
if value is None:
29+
return value
2830
field_name = getattr(info, "field_name", "time")
2931
_validate_hhmm(value, field_name)
3032
return value
@@ -33,27 +35,27 @@ def validate_hhmm(cls, value: str, info: object) -> str:
3335
class AdministrativeUnit(NetworkModel):
3436
"""Administrative unit where the retail entity is located."""
3537

36-
settlement_type: Literal["village", "city"]
37-
name: str
38-
alias: str
39-
country: Literal["BLR", "RUS", "USA", "ARE"]
40-
region: str
41-
longitude: float
42-
latitude: float
38+
settlement_type: Literal["village", "city"] | None = None
39+
name: str | None = None
40+
alias: str | None = None
41+
country: Literal["BLR", "RUS", "USA", "ARE"] | None = None
42+
region: str | None = None
43+
longitude: float | None = None
44+
latitude: float | None = None
4345

4446

4547
class RetailUnit(NetworkModel):
4648
"""Retail entity: store, pickup point, or warehouse."""
4749

48-
retail_type: Literal["pickup_point", "store", "warehouse"]
49-
code: str
50-
address: str
51-
schedule_weekdays: Schedule
52-
schedule_saturday: Schedule
53-
schedule_sunday: Schedule
54-
temporarily_closed: bool
55-
longitude: float
56-
latitude: float
57-
administrative_unit: AdministrativeUnit
58-
categories: list[Category] = Field(default_factory=list)
59-
products: list[Card] = Field(default_factory=list)
50+
retail_type: Literal["pickup_point", "store", "warehouse"] | None = None
51+
code: str | None = None
52+
address: str | None = None
53+
schedule_weekdays: Schedule | None = None
54+
schedule_saturday: Schedule | None = None
55+
schedule_sunday: Schedule | None = None
56+
temporarily_closed: bool | None = None
57+
longitude: float | None = None
58+
latitude: float | None = None
59+
administrative_unit: AdministrativeUnit | None = None
60+
categories: list[Category] | None = Field(default_factory=list)
61+
products: list[Card] | None = Field(default_factory=list)

0 commit comments

Comments
 (0)