Skip to content

fix: improve parameter definition validation and error reporting #184

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: mainline
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 37 additions & 12 deletions src/openjd/model/v2023_09/_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1187,6 +1187,10 @@ def _validate_max_length(cls, value: Optional[int], info: ValidationInfo) -> Opt
def _validate_allowed_values_item(
cls, value: AllowedParameterStringValueList, info: ValidationInfo
) -> AllowedParameterStringValueList:
if value is None:
raise ValueError(
"allowedValues cannot be None. The field must contain at least one value or be omitted entirely."
)
min_length = info.data.get("minLength")
max_length = info.data.get("maxLength")
errors = list[InitErrorDetails]()
Expand Down Expand Up @@ -1397,7 +1401,7 @@ class JobPathParameterDefinition(OpenJDModel_v2023_09, JobParameterInterface):
"default",
},
adds_fields=lambda this, symtab: {
"value": symtab[f"RawParam.{cast(JobStringParameterDefinition,this).name}"]
"value": symtab[f"RawParam.{cast(JobPathParameterDefinition,this).name}"]
},
)

Expand Down Expand Up @@ -1427,8 +1431,12 @@ def _validate_max_length(cls, value: Optional[int], info: ValidationInfo) -> Opt
@field_validator("allowedValues")
@classmethod
def _validate_allowed_values_item(
cls, value: ParameterStringValue, info: ValidationInfo
) -> ParameterStringValue:
cls, value: AllowedParameterStringValueList, info: ValidationInfo
) -> AllowedParameterStringValueList:
if value is None:
raise ValueError(
"allowedValues cannot be None. The field must contain at least one value or be omitted entirely."
)
min_length = info.data.get("minLength")
max_length = info.data.get("maxLength")
errors = list[InitErrorDetails]()
Expand All @@ -1438,7 +1446,7 @@ def _validate_allowed_values_item(
errors.append(
InitErrorDetails(
type="value_error",
loc=("allowedValues", i),
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before, the error included allowedValues twice for JobPathParameterDefinition:

'loc': ('allowedValues', 'allowedValues', 2)

After:

('allowedValues', 2)

This is now also consistent with JobStringParameterDefinition and other job parameter types as well.

loc=(i,),
ctx={"error": ValueError("Value is shorter than minLength.")},
input=item,
)
Expand All @@ -1448,7 +1456,7 @@ def _validate_allowed_values_item(
errors.append(
InitErrorDetails(
type="value_error",
loc=("allowedValues", i),
loc=(i,),
ctx={"error": ValueError("Value is longer than maxLength.")},
input=item,
)
Expand Down Expand Up @@ -1586,7 +1594,7 @@ class JobIntParameterDefinition(OpenJDModel_v2023_09):
allowedValues (Optional[AllowedIntParameterList]): Explicit list of values that the
parameter is allowed to take on.
minValue (Optional[int]): Minimum value that the parameter is allowed to be.
maxValue (Optional[int]): Minimum value that the parameter is allowed to be.
maxValue (Optional[int]): Maximum value that the parameter is allowed to be.
"""

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

@field_validator("allowedValues", mode="before")
@classmethod
def _validate_allowed_values_item_type(cls, value: Any) -> Any:
def _validate_allowed_values_item_type(
cls, value: AllowedIntParameterList
) -> AllowedIntParameterList:
if value is None:
raise ValueError(
"allowedValues cannot be None. The field must contain at least one value or be omitted entirely."
)

errors = list[InitErrorDetails]()
for i, item in enumerate(value):
if isinstance(item, bool) or not isinstance(item, (int, str)):
Expand Down Expand Up @@ -1688,7 +1703,13 @@ def _validate_max_value(cls, value: Optional[int], info: ValidationInfo) -> Opti

@field_validator("allowedValues")
@classmethod
def _validate_allowed_values_item(cls, value: list[int], info: ValidationInfo) -> list[int]:
def _validate_allowed_values_item(
cls, value: AllowedIntParameterList, info: ValidationInfo
) -> AllowedIntParameterList:
if value is None:
raise ValueError(
"allowedValues cannot be None. The field must contain at least one value or be omitted entirely."
)
min_value = info.data.get("minValue")
max_value = info.data.get("maxValue")
errors = list[InitErrorDetails]()
Expand All @@ -1709,7 +1730,7 @@ def _validate_allowed_values_item(cls, value: list[int], info: ValidationInfo) -
InitErrorDetails(
type="value_error",
loc=(i,),
ctx={"error": ValueError("Value larger than minValue.")},
ctx={"error": ValueError("Value larger than maxValue.")},
input=item,
)
)
Expand Down Expand Up @@ -1832,7 +1853,7 @@ class JobFloatParameterDefinition(OpenJDModel_v2023_09):
allowedValues (Optional[AllowedFloatParameterList]): Explicit list of values that the
parameter is allowed to take on.
minValue (Optional[Decimal]): Minimum value that the parameter is allowed to be.
maxValue (Optional[Decimal]): Minimum value that the parameter is allowed to be.
maxValue (Optional[Decimal]): Maximum value that the parameter is allowed to be.
"""

name: Identifier
Expand Down Expand Up @@ -1885,8 +1906,12 @@ def _validate_max_value(
@field_validator("allowedValues")
@classmethod
def _validate_allowed_values_item(
cls, value: list[Decimal], info: ValidationInfo
) -> list[Decimal]:
cls, value: AllowedFloatParameterList, info: ValidationInfo
) -> AllowedFloatParameterList:
if value is None:
raise ValueError(
"allowedValues cannot be None. The field must contain at least one value or be omitted entirely."
)
min_value = info.data.get("minValue")
max_value = info.data.get("maxValue")
errors = list[InitErrorDetails]()
Expand Down
Loading