File tree Expand file tree Collapse file tree 2 files changed +46
-0
lines changed
Expand file tree Collapse file tree 2 files changed +46
-0
lines changed Original file line number Diff line number Diff line change @@ -846,6 +846,22 @@ class Mapping(BaseModel):
846846 populate_by_name = True , extra = "allow" , validate_assignment = True
847847 )
848848
849+ @model_validator (mode = "after" )
850+ def _validate (self ) -> Self :
851+ if (
852+ self .model_id
853+ and self .model_id != self .petab_id
854+ and is_valid_identifier (self .model_id )
855+ ):
856+ raise ValueError (
857+ "Aliasing of entities that already have a valid identifier "
858+ "is not allowed. Simplify your PEtab problem by removing the "
859+ f"mapping entry for `{ self .petab_id } -> { self .model_id } `, "
860+ f"and replacing all occurrences of `{ self .petab_id } ` with "
861+ f"`{ self .model_id } `."
862+ )
863+ return self
864+
849865
850866class MappingTable (BaseTable [Mapping ]):
851867 """PEtab mapping table."""
Original file line number Diff line number Diff line change @@ -836,3 +836,33 @@ def test_get_output_parameters():
836836 assert petab_problem .get_output_parameters (
837837 observable = False , noise = True
838838 ) == ["p1" , "p3" , "p5" ]
839+
840+
841+ def test_mapping_validation ():
842+ """Test that invalid mapping entries raise errors."""
843+
844+ # alias invalid model entity ID
845+ Mapping (
846+ petab_id = "valid_id" ,
847+ model_id = " 1_invalid" ,
848+ )
849+
850+ with pytest .raises (ValidationError , match = "Invalid ID" ):
851+ # invalid petab entity ID
852+ Mapping (
853+ petab_id = "1_invalid" ,
854+ model_id = "valid_id" ,
855+ )
856+
857+ with pytest .raises (ValidationError , match = "Aliasing.*not allowed" ):
858+ # unnecessary aliasing is forbidden
859+ Mapping (
860+ petab_id = "forbidden_alias_of_valid_id" ,
861+ model_id = "valid_id" ,
862+ )
863+
864+ # missing model_id is valid (annotation-only entry)
865+ Mapping (petab_id = "valid_id" , name = "some name" )
866+
867+ # identity mapping is valid
868+ Mapping (petab_id = "valid_id" , model_id = "valid_id" , name = "some name" )
You can’t perform that action at this time.
0 commit comments