|
7 | 7 | from pathlib import Path |
8 | 8 | from typing import Any, Dict, Optional, Type |
9 | 9 |
|
10 | | -from pydantic import BaseModel, Field, field_validator, model_validator |
| 10 | +from pydantic import BaseModel, Field, field_validator, model_validator, ValidationInfo |
11 | 11 |
|
12 | 12 | from easyswitch.exceptions import ConfigurationError |
13 | 13 | from easyswitch.types import Currency, Provider |
@@ -61,7 +61,7 @@ class BaseConfigModel(BaseModel): |
61 | 61 |
|
62 | 62 | class Config: |
63 | 63 | extra = 'forbid' # Undefined fields are not allowed |
64 | | - validate_all = True |
| 64 | + validate_default = True |
65 | 65 | use_enum_values = True |
66 | 66 |
|
67 | 67 |
|
@@ -127,17 +127,20 @@ class RootConfig(BaseConfigModel): |
127 | 127 | default_provider: Optional[Provider] = None |
128 | 128 |
|
129 | 129 | @field_validator('default_provider') |
130 | | - def validate_default_provider(cls, v, values): |
| 130 | + @classmethod |
| 131 | + def validate_default_provider(cls, v, info: ValidationInfo): |
131 | 132 | """Ensure default provider is valid.""" |
132 | | - |
| 133 | + |
133 | 134 | # Ensure default provider is in enabled providers |
134 | | - if v is not None and 'providers' in values and v not in values['providers']: |
135 | | - raise ValueError( |
136 | | - f"Default provider {v} must be in enabled providers" |
137 | | - ) |
| 135 | + if v is not None: |
| 136 | + providers = info.data.get('providers') |
| 137 | + if providers and v not in providers: |
| 138 | + raise ValueError( |
| 139 | + f"Default provider {v} must be in enabled providers" |
| 140 | + ) |
138 | 141 |
|
139 | 142 | # and in supported Providers |
140 | | - if v not in Provider.__members__: |
| 143 | + if v is not None and v not in Provider.__members__: |
141 | 144 | raise ValueError( |
142 | 145 | f"Default provider {v} is not supported" |
143 | 146 | ) |
|
0 commit comments