|
33 | 33 | } |
34 | 34 |
|
35 | 35 | values_defined_in = { |
36 | | - "backgrounds.value_1": "background_parameters", |
37 | | - "backgrounds.value_2": "background_parameters", |
38 | | - "backgrounds.value_3": "background_parameters", |
39 | | - "backgrounds.value_4": "background_parameters", |
40 | | - "backgrounds.value_5": "background_parameters", |
41 | | - "resolutions.value_1": "resolution_parameters", |
42 | | - "resolutions.value_2": "resolution_parameters", |
43 | | - "resolutions.value_3": "resolution_parameters", |
44 | | - "resolutions.value_4": "resolution_parameters", |
45 | | - "resolutions.value_5": "resolution_parameters", |
| 36 | + "backgrounds.constant.value_1": "background_parameters", |
| 37 | + "backgrounds.constant.value_2": "background_parameters", |
| 38 | + "backgrounds.constant.value_3": "background_parameters", |
| 39 | + "backgrounds.constant.value_4": "background_parameters", |
| 40 | + "backgrounds.constant.value_5": "background_parameters", |
| 41 | + "backgrounds.data.value_1": "data", |
| 42 | + "backgrounds.data.value_2": "data", |
| 43 | + "backgrounds.data.value_3": "data", |
| 44 | + "backgrounds.data.value_4": "data", |
| 45 | + "backgrounds.data.value_5": "data", |
| 46 | + "resolutions.constant.value_1": "resolution_parameters", |
| 47 | + "resolutions.constant.value_2": "resolution_parameters", |
| 48 | + "resolutions.constant.value_3": "resolution_parameters", |
| 49 | + "resolutions.constant.value_4": "resolution_parameters", |
| 50 | + "resolutions.constant.value_5": "resolution_parameters", |
| 51 | + "resolutions.data.value_1": "data", |
| 52 | + "resolutions.data.value_2": "data", |
| 53 | + "resolutions.data.value_3": "data", |
| 54 | + "resolutions.data.value_4": "data", |
| 55 | + "resolutions.data.value_5": "data", |
46 | 56 | "layers.thickness": "parameters", |
47 | 57 | "layers.SLD": "parameters", |
48 | 58 | "layers.SLD_real": "parameters", |
@@ -434,8 +444,13 @@ def update_renamed_models(self) -> "Project": |
434 | 444 | def cross_check_model_values(self) -> "Project": |
435 | 445 | """Certain model fields should contain values defined elsewhere in the project.""" |
436 | 446 | value_fields = ["value_1", "value_2", "value_3", "value_4", "value_5"] |
437 | | - self.check_allowed_values("backgrounds", value_fields, self.background_parameters.get_names()) |
438 | | - self.check_allowed_values("resolutions", value_fields, self.resolution_parameters.get_names()) |
| 447 | + self.check_allowed_background_resolution_values( |
| 448 | + "backgrounds", value_fields, self.background_parameters.get_names(), self.data.get_names() |
| 449 | + ) |
| 450 | + self.check_allowed_background_resolution_values( |
| 451 | + "resolutions", value_fields, self.resolution_parameters.get_names(), self.data.get_names() |
| 452 | + ) |
| 453 | + |
439 | 454 | self.check_allowed_values( |
440 | 455 | "layers", |
441 | 456 | ["thickness", "SLD", "SLD_real", "SLD_imaginary", "roughness"], |
@@ -526,6 +541,49 @@ def check_allowed_values(self, attribute: str, field_list: list[str], allowed_va |
526 | 541 | f'"{values_defined_in[f"{attribute}.{field}"]}".', |
527 | 542 | ) |
528 | 543 |
|
| 544 | + def check_allowed_background_resolution_values( |
| 545 | + self, attribute: str, field_list: list[str], allowed_constants: list[str], allowed_data: list[str] |
| 546 | + ) -> None: |
| 547 | + """Check the values of the given fields in the given model are in the supplied list of allowed values. |
| 548 | +
|
| 549 | + For backgrounds and resolutions, the list of allowed values depends on whether the type of the |
| 550 | + background/resolution is "constant" or "data". |
| 551 | +
|
| 552 | + Parameters |
| 553 | + ---------- |
| 554 | + attribute : str |
| 555 | + The attribute of Project being validated. |
| 556 | + field_list : list [str] |
| 557 | + The fields of the attribute to be checked for valid values. |
| 558 | + allowed_constants : list [str] |
| 559 | + The list of allowed values for the fields given in field_list if the type is "constant". |
| 560 | + allowed_data : list [str] |
| 561 | + The list of allowed values for the fields given in field_list if the type is "data". |
| 562 | +
|
| 563 | + Raises |
| 564 | + ------ |
| 565 | + ValueError |
| 566 | + Raised if any field in field_list has a value not specified in allowed_constants or allowed_data as |
| 567 | + appropriate. |
| 568 | +
|
| 569 | + """ |
| 570 | + class_list = getattr(self, attribute) |
| 571 | + for model in class_list: |
| 572 | + if model.type == TypeOptions.Constant: |
| 573 | + allowed_values = allowed_constants |
| 574 | + elif model.type == TypeOptions.Data: |
| 575 | + allowed_values = allowed_data |
| 576 | + else: |
| 577 | + raise ValueError('"Function" type backgrounds and resolutions are not yet supported.') |
| 578 | + |
| 579 | + for field in field_list: |
| 580 | + value = getattr(model, field, "") |
| 581 | + if value and value not in allowed_values: |
| 582 | + raise ValueError( |
| 583 | + f'The value "{value}" in the "{field}" field of "{attribute}" must be defined in ' |
| 584 | + f'"{values_defined_in[f"{attribute}.{model.type}.{field}"]}".', |
| 585 | + ) |
| 586 | + |
529 | 587 | def check_contrast_model_allowed_values( |
530 | 588 | self, |
531 | 589 | contrast_attribute: str, |
@@ -648,7 +706,7 @@ def wrapped_func(*args, **kwargs): |
648 | 706 | except ValidationError as exc: |
649 | 707 | class_list.data = previous_state |
650 | 708 | custom_error_list = custom_pydantic_validation_error(exc.errors()) |
651 | | - raise ValidationError.from_exception_data(exc.title, custom_error_list) from None |
| 709 | + raise ValidationError.from_exception_data(exc.title, custom_error_list, hide_input=True) from None |
652 | 710 | except (TypeError, ValueError): |
653 | 711 | class_list.data = previous_state |
654 | 712 | raise |
|
0 commit comments