2
2
Pydantic Schemas for Dashboard Configuration Validation
3
3
"""
4
4
5
- from pydantic import BaseModel , Field , validator
6
- from typing import List , Dict , Optional , Any
5
+ from pydantic import BaseModel , Field , field_validator , model_validator
6
+ from typing import List , Dict , Optional
7
7
8
8
9
9
class PrimaryMetricConfig (BaseModel ):
@@ -15,7 +15,8 @@ class PrimaryMetricConfig(BaseModel):
15
15
description = "Optional source column to calculate the primary metric from if it doesn't exist." ,
16
16
)
17
17
18
- @validator ("goal" )
18
+ @field_validator ("goal" )
19
+ @classmethod
19
20
def goal_must_be_max_or_min (cls , v : str ) -> str :
20
21
"""Validates that the goal is either 'maximize' or 'minimize'."""
21
22
if v not in ["maximize" , "minimize" ]:
@@ -65,17 +66,15 @@ class BarPlotConfig(BaseModel):
65
66
y_columns : Optional [List [str ]] = None
66
67
series : Optional [List [StackedBarPlotSeries ]] = None
67
68
68
- @validator ("y_columns" , always = True )
69
- def check_prefix_or_columns (
70
- cls , v : Optional [List [str ]], values : Dict [str , Any ]
71
- ) -> Optional [List [str ]]:
69
+ @model_validator (mode = "after" )
70
+ def check_prefix_or_columns (self ) -> "BarPlotConfig" :
72
71
"""Validates that either 'y_prefix' or 'y_columns' is provided for grouped_bar plots."""
73
- if not values . get ( " y_prefix" ) and not v :
74
- if values . get ( " type" ) == "grouped_bar" :
72
+ if not self . y_prefix and not self . y_columns :
73
+ if self . type == "grouped_bar" :
75
74
raise ValueError (
76
75
"Either 'y_prefix' or 'y_columns' must be provided for grouped_bar plots."
77
76
)
78
- return v
77
+ return self
79
78
80
79
81
80
class PlotConfig (BaseModel ):
0 commit comments