|
1 | | -from pydantic import BaseModel, ConfigDict |
| 1 | +from pydantic import BaseModel, ConfigDict, model_validator |
| 2 | +from typing import Any |
| 3 | + |
| 4 | +# Valid parameter prefixes, their expected format and description |
| 5 | +_PARAMETER_PREFIXES = { |
| 6 | + "tilt": ("tiltN", "Tilt"), |
| 7 | + "En": ("EnN", "Normal component"), |
| 8 | + "Es": ("EsN", "Skew component"), |
| 9 | +} |
| 10 | + |
| 11 | + |
| 12 | +def _validate_order( |
| 13 | + key_num: str, parameter_name: str, prefix: str, expected_format: str |
| 14 | +) -> None: |
| 15 | + """Validate that the order number is a non-negative integer without leading zeros.""" |
| 16 | + error_msg = ( |
| 17 | + f"Invalid {parameter_name}: '{prefix}{key_num}'. " |
| 18 | + f"Parameter must be of the form '{expected_format}', where 'N' is a non-negative integer without leading zeros." |
| 19 | + ) |
| 20 | + if not key_num.isdigit() or (key_num.startswith("0") and key_num != "0"): |
| 21 | + raise ValueError(error_msg) |
2 | 22 |
|
3 | 23 |
|
4 | 24 | class ElectricMultipoleParameters(BaseModel): |
5 | | - """Electric multipole parameters""" |
| 25 | + """Electric multipole parameters |
| 26 | +
|
| 27 | + Valid parameter formats: |
| 28 | + - tiltN: Tilt of Nth order multipole |
| 29 | + - EnN: Normal component of Nth order multipole |
| 30 | + - EsN: Skew component of Nth order multipole |
| 31 | + - *NL: Length-integrated versions of components (e.g., En3L, EsNL) |
| 32 | +
|
| 33 | + Where N is a positive integer without leading zeros (except "0" itself). |
| 34 | + """ |
6 | 35 |
|
7 | | - # Allow arbitrary fields (TODO: remove this) |
8 | 36 | model_config = ConfigDict(extra="allow") |
9 | 37 |
|
10 | | - # TODO: add ElectricMultipoleParameters in a follow-up RP |
11 | | - # https://pals-project.readthedocs.io/en/latest/element-parameters.html#electricmultipolep-electric-multipole-parameters |
| 38 | + @model_validator(mode="before") |
| 39 | + @classmethod |
| 40 | + def validate(cls, values: dict[str, Any]) -> dict[str, Any]: |
| 41 | + """Validate all parameter names match the expected multipole format.""" |
| 42 | + for key in values: |
| 43 | + # Check if key ends with 'L' for length-integrated values |
| 44 | + is_length_integrated = key.endswith("L") |
| 45 | + base_key = key[:-1] if is_length_integrated else key |
| 46 | + |
| 47 | + # No length-integrated values allowed for tilt parameter |
| 48 | + if is_length_integrated and base_key.startswith("tilt"): |
| 49 | + raise ValueError(f"Invalid electric multipole parameter: '{key}'. ") |
| 50 | + |
| 51 | + # Find matching prefix |
| 52 | + for prefix, (expected_format, description) in _PARAMETER_PREFIXES.items(): |
| 53 | + if base_key.startswith(prefix): |
| 54 | + key_num = base_key[len(prefix) :] |
| 55 | + _validate_order(key_num, description, prefix, expected_format) |
| 56 | + break |
| 57 | + else: |
| 58 | + raise ValueError( |
| 59 | + f"Invalid electric multipole parameter: '{key}'. " |
| 60 | + f"Parameters must be of the form 'tiltN', 'EnN', or 'EsN' " |
| 61 | + f"(with optional 'L' suffix for length-integrated), where 'N' is a non-negative integer." |
| 62 | + ) |
| 63 | + return values |
0 commit comments