Skip to content

Commit 95efd47

Browse files
author
Andrei Neagu
committed
refactor and fix model
1 parent 86978d5 commit 95efd47

File tree

2 files changed

+8
-25
lines changed

2 files changed

+8
-25
lines changed

packages/models-library/src/models_library/api_schemas_dynamic_sidecar/telemetry.py

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
Field,
99
NonNegativeFloat,
1010
NonNegativeInt,
11-
root_validator,
12-
validator,
11+
model_validator,
1312
)
1413

1514
from ..projects_nodes_io import NodeID
@@ -56,29 +55,13 @@ class DiskUsage(BaseModel):
5655
free: ByteSize = Field(description="remaining space")
5756

5857
total: ByteSize = Field(description="total space = free + used")
59-
used_percent: NonNegativeFloat = Field(
58+
used_percent: float = Field(
6059
ge=0.00,
6160
le=100.00,
62-
description="Percent of used space relative to the total space",
61+
description="Percent of used space ree:ative to the total space",
6362
)
6463

65-
@validator("free")
66-
@classmethod
67-
def _free_positive(cls, v: float) -> float:
68-
if v < 0:
69-
msg = f"free={v} cannot be a negative value"
70-
raise ValueError(msg)
71-
return v
72-
73-
@validator("used")
74-
@classmethod
75-
def _used_positive(cls, v: float) -> float:
76-
if v < 0:
77-
msg = f"used={v} cannot be a negative value"
78-
raise ValueError(msg)
79-
return v
80-
81-
@root_validator(pre=True)
64+
@model_validator(mode="before")
8265
@classmethod
8366
def _check_total(cls, values: dict[str, Any]) -> dict[str, Any]:
8467
total = values["total"]

packages/models-library/tests/test_api_schemas_dynamic_sidecar_telemetry.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ def test_failing_validation():
4242
with pytest.raises(ValidationError) as exc:
4343
assert DiskUsage.from_efs_guardian(100, 10)
4444

45-
assert "free=" in f"{exc.value}"
46-
assert "negative value" in f"{exc.value}"
45+
assert "free" in f"{exc.value}"
46+
assert "input_value=-90" in f"{exc.value}"
4747

4848
with pytest.raises(ValidationError) as exc:
4949
assert DiskUsage(
@@ -52,8 +52,8 @@ def test_failing_validation():
5252
total=ByteSize(0),
5353
used_percent=-10,
5454
)
55-
assert "used=" in f"{exc.value}"
56-
assert "negative value" in f"{exc.value}"
55+
assert "used" in f"{exc.value}"
56+
assert "input_value=-10" in f"{exc.value}"
5757

5858
with pytest.raises(ValidationError) as exc:
5959
DiskUsage(

0 commit comments

Comments
 (0)