Skip to content

Commit 60d6ea2

Browse files
committed
fix: improve parameter definition validation and error reporting
This commit includes several improvements to parameter validation: 1. Add explicit validation for None values in allowedValues across all parameter types 2. Fix error location reporting in validation error messages 3. Correct documentation comments for maxValue parameters 4. Fix type casting in JobPathParameterDefinition 5. Add test cases for both implicit and explicit None handling The changes ensure that explicitly set None values for allowedValues are properly rejected with clear error messages, while implicitly omitted allowedValues continue to work correctly. Signed-off-by: Roman Yakobenchuk <66849711+ryyakobe@users.noreply.github.com>
1 parent 4f75188 commit 60d6ea2

2 files changed

Lines changed: 289 additions & 12 deletions

File tree

src/openjd/model/v2023_09/_model.py

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,6 +1187,10 @@ def _validate_max_length(cls, value: Optional[int], info: ValidationInfo) -> Opt
11871187
def _validate_allowed_values_item(
11881188
cls, value: AllowedParameterStringValueList, info: ValidationInfo
11891189
) -> AllowedParameterStringValueList:
1190+
if value is None:
1191+
raise ValueError(
1192+
"allowedValues cannot be None. The field must contain at least one value or be omitted entirely."
1193+
)
11901194
min_length = info.data.get("minLength")
11911195
max_length = info.data.get("maxLength")
11921196
errors = list[InitErrorDetails]()
@@ -1397,7 +1401,7 @@ class JobPathParameterDefinition(OpenJDModel_v2023_09, JobParameterInterface):
13971401
"default",
13981402
},
13991403
adds_fields=lambda this, symtab: {
1400-
"value": symtab[f"RawParam.{cast(JobStringParameterDefinition,this).name}"]
1404+
"value": symtab[f"RawParam.{cast(JobPathParameterDefinition,this).name}"]
14011405
},
14021406
)
14031407

@@ -1427,8 +1431,12 @@ def _validate_max_length(cls, value: Optional[int], info: ValidationInfo) -> Opt
14271431
@field_validator("allowedValues")
14281432
@classmethod
14291433
def _validate_allowed_values_item(
1430-
cls, value: ParameterStringValue, info: ValidationInfo
1431-
) -> ParameterStringValue:
1434+
cls, value: AllowedParameterStringValueList, info: ValidationInfo
1435+
) -> AllowedParameterStringValueList:
1436+
if value is None:
1437+
raise ValueError(
1438+
"allowedValues cannot be None. The field must contain at least one value or be omitted entirely."
1439+
)
14321440
min_length = info.data.get("minLength")
14331441
max_length = info.data.get("maxLength")
14341442
errors = list[InitErrorDetails]()
@@ -1438,7 +1446,7 @@ def _validate_allowed_values_item(
14381446
errors.append(
14391447
InitErrorDetails(
14401448
type="value_error",
1441-
loc=("allowedValues", i),
1449+
loc=(i,),
14421450
ctx={"error": ValueError("Value is shorter than minLength.")},
14431451
input=item,
14441452
)
@@ -1448,7 +1456,7 @@ def _validate_allowed_values_item(
14481456
errors.append(
14491457
InitErrorDetails(
14501458
type="value_error",
1451-
loc=("allowedValues", i),
1459+
loc=(i,),
14521460
ctx={"error": ValueError("Value is longer than maxLength.")},
14531461
input=item,
14541462
)
@@ -1586,7 +1594,7 @@ class JobIntParameterDefinition(OpenJDModel_v2023_09):
15861594
allowedValues (Optional[AllowedIntParameterList]): Explicit list of values that the
15871595
parameter is allowed to take on.
15881596
minValue (Optional[int]): Minimum value that the parameter is allowed to be.
1589-
maxValue (Optional[int]): Minimum value that the parameter is allowed to be.
1597+
maxValue (Optional[int]): Maximum value that the parameter is allowed to be.
15901598
"""
15911599

15921600
name: Identifier
@@ -1647,7 +1655,14 @@ def _validate_max_value_type(cls, value: Optional[Any]) -> Optional[Any]:
16471655

16481656
@field_validator("allowedValues", mode="before")
16491657
@classmethod
1650-
def _validate_allowed_values_item_type(cls, value: Any) -> Any:
1658+
def _validate_allowed_values_item_type(
1659+
cls, value: AllowedIntParameterList
1660+
) -> AllowedIntParameterList:
1661+
if value is None:
1662+
raise ValueError(
1663+
"allowedValues cannot be None. The field must contain at least one value or be omitted entirely."
1664+
)
1665+
16511666
errors = list[InitErrorDetails]()
16521667
for i, item in enumerate(value):
16531668
if isinstance(item, bool) or not isinstance(item, (int, str)):
@@ -1688,7 +1703,13 @@ def _validate_max_value(cls, value: Optional[int], info: ValidationInfo) -> Opti
16881703

16891704
@field_validator("allowedValues")
16901705
@classmethod
1691-
def _validate_allowed_values_item(cls, value: list[int], info: ValidationInfo) -> list[int]:
1706+
def _validate_allowed_values_item(
1707+
cls, value: AllowedIntParameterList, info: ValidationInfo
1708+
) -> AllowedIntParameterList:
1709+
if value is None:
1710+
raise ValueError(
1711+
"allowedValues cannot be None. The field must contain at least one value or be omitted entirely."
1712+
)
16921713
min_value = info.data.get("minValue")
16931714
max_value = info.data.get("maxValue")
16941715
errors = list[InitErrorDetails]()
@@ -1709,7 +1730,7 @@ def _validate_allowed_values_item(cls, value: list[int], info: ValidationInfo) -
17091730
InitErrorDetails(
17101731
type="value_error",
17111732
loc=(i,),
1712-
ctx={"error": ValueError("Value larger than minValue.")},
1733+
ctx={"error": ValueError("Value larger than maxValue.")},
17131734
input=item,
17141735
)
17151736
)
@@ -1832,7 +1853,7 @@ class JobFloatParameterDefinition(OpenJDModel_v2023_09):
18321853
allowedValues (Optional[AllowedFloatParameterList]): Explicit list of values that the
18331854
parameter is allowed to take on.
18341855
minValue (Optional[Decimal]): Minimum value that the parameter is allowed to be.
1835-
maxValue (Optional[Decimal]): Minimum value that the parameter is allowed to be.
1856+
maxValue (Optional[Decimal]): Maximum value that the parameter is allowed to be.
18361857
"""
18371858

18381859
name: Identifier
@@ -1885,8 +1906,12 @@ def _validate_max_value(
18851906
@field_validator("allowedValues")
18861907
@classmethod
18871908
def _validate_allowed_values_item(
1888-
cls, value: list[Decimal], info: ValidationInfo
1889-
) -> list[Decimal]:
1909+
cls, value: AllowedFloatParameterList, info: ValidationInfo
1910+
) -> AllowedFloatParameterList:
1911+
if value is None:
1912+
raise ValueError(
1913+
"allowedValues cannot be None. The field must contain at least one value or be omitted entirely."
1914+
)
18901915
min_value = info.data.get("minValue")
18911916
max_value = info.data.get("maxValue")
18921917
errors = list[InitErrorDetails]()

0 commit comments

Comments
 (0)