poc of validate config (#24837)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
@@ -1,3 +1,14 @@
|
|||||||
|
from pydantic import BaseModel, Field, ValidationError
|
||||||
|
|
||||||
|
|
||||||
|
class MoreLikeThisConfig(BaseModel):
|
||||||
|
enabled: bool = False
|
||||||
|
|
||||||
|
|
||||||
|
class AppConfigModel(BaseModel):
|
||||||
|
more_like_this: MoreLikeThisConfig = Field(default_factory=MoreLikeThisConfig)
|
||||||
|
|
||||||
|
|
||||||
class MoreLikeThisConfigManager:
|
class MoreLikeThisConfigManager:
|
||||||
@classmethod
|
@classmethod
|
||||||
def convert(cls, config: dict) -> bool:
|
def convert(cls, config: dict) -> bool:
|
||||||
@@ -6,31 +17,14 @@ class MoreLikeThisConfigManager:
|
|||||||
|
|
||||||
:param config: model config args
|
:param config: model config args
|
||||||
"""
|
"""
|
||||||
more_like_this = False
|
validated_config, _ = cls.validate_and_set_defaults(config)
|
||||||
more_like_this_dict = config.get("more_like_this")
|
return AppConfigModel.model_validate(validated_config).more_like_this.enabled
|
||||||
if more_like_this_dict:
|
|
||||||
if more_like_this_dict.get("enabled"):
|
|
||||||
more_like_this = True
|
|
||||||
|
|
||||||
return more_like_this
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def validate_and_set_defaults(cls, config: dict) -> tuple[dict, list[str]]:
|
def validate_and_set_defaults(cls, config: dict) -> tuple[dict, list[str]]:
|
||||||
"""
|
try:
|
||||||
Validate and set defaults for more like this feature
|
return AppConfigModel.model_validate(config).dict(), ["more_like_this"]
|
||||||
|
except ValidationError as e:
|
||||||
:param config: app model config args
|
raise ValueError(
|
||||||
"""
|
"more_like_this must be of dict type and enabled in more_like_this must be of boolean type"
|
||||||
if not config.get("more_like_this"):
|
)
|
||||||
config["more_like_this"] = {"enabled": False}
|
|
||||||
|
|
||||||
if not isinstance(config["more_like_this"], dict):
|
|
||||||
raise ValueError("more_like_this must be of dict type")
|
|
||||||
|
|
||||||
if "enabled" not in config["more_like_this"] or not config["more_like_this"]["enabled"]:
|
|
||||||
config["more_like_this"]["enabled"] = False
|
|
||||||
|
|
||||||
if not isinstance(config["more_like_this"]["enabled"], bool):
|
|
||||||
raise ValueError("enabled in more_like_this must be of boolean type")
|
|
||||||
|
|
||||||
return config, ["more_like_this"]
|
|
||||||
|
Reference in New Issue
Block a user