Deltares / HYDROLIB-core

Core code around the I/O of the DHYDRO-suite
https://deltares.github.io/HYDROLIB-core/
MIT License
24 stars 4 forks source link

Empty partitionFile field in MDU gives wrong validation error #208

Closed arthurvd closed 2 years ago

arthurvd commented 2 years ago

Describe the bug When reading an FMModel where the MDU contains empty partitionFile line, an incorrect validation error occurs ("not a valid dict").

To Reproduce

  1. Use our base model in 'tests/data/input/e02/f152_1d2d_projectmodels_rhu/c04_DHydamo-MGB-initialisation/fm/', the MDU contains this line:
    PartitionFile                     =                     # Domain partition polygon file *_part.pol for parallel run
  2. load MDU using:
    
    from hydrolib.core.io.mdu.models import FMModel
    modeldir = Path(
    "c:/checkouts/HYDROLIB-core_git/tests/data/input/e02/f152_1d2d_projectmodels_rhu/c04_DHydamo-MGB-initialisation/fm/"
    )
    mdufile = "moergestels_broek.mdu"

Read mdu model

fm = FMModel(modeldir / mdufile)

2. Running this will produce a validation error:

Exception has occurred: ValidationError 1 validation error for FMModel moergestels_broek.mdu -> geometry -> partitionFile value is not a valid dict (type=type_error.dict) File "C:\checkouts\HYDROLIB-core_git\hydrolib\core\basemodel.py", line 48, in init super().init(**data)


**Expected behavior**
Validation error should not occur, since this field is optional:
```python
    partitionfile: Optional[PolyFile] = Field(None, alias="partitionFile")

Possibly this originates from the fact that class PolyFile is involved, most other optional file fields are of type Optional[Path]

Version info (please complete the following information):

evetion commented 2 years ago

This occurs because partitionFile occurs twice:

PartitionFile                     =                     # Domain partition polygon file *_part.pol for parallel run
AngLat                            = 0.                  # Angle of latitude S-N (deg), 0: no Coriolis
AngLon                            = 0.                  # Angle of longitude E-W (deg), 0: Greenwich, used in solar heat flux computation.
Conveyance2D                      = -1                  # -1: R=HU,0: R=H, 1: R=A/P, 2: K=analytic-1D conv, 3: K=analytic-2D conv
Slotw2D                           = 0.                  # -
NonLin1D                          = 0
1D2DLinkFile                      = # File containing the custom parameterization of 1D-2D links.
PartitionFile                     = # *_part.pol, polyline(s) x,y
arthurvd commented 2 years ago

Correct diagnosis. I wonder how we can improve on the error message. The **data contains a list value for the duplicate input (often [None, None] if both were empty), but it depends on the Field's datatype what kind of validation error is given. For example for this field:

    waterlevinifile: Optional[Path] = Field(None, alias="waterLevIniFile")

the error when passing duplicate lines in input is:

geometry -> waterLevIniFile
  value is not a valid path (type=type_error.path)

When doing the same for a model class type, e.g.:

    crosslocfile: Optional[CrossLocModel] = Field(None, alias="crossLocFile")
    partitionfile: Optional[PolyFile] = Field(None, alias="partitionFile")

the error when passing duplicate lines in input is:

geometry -> crossLocFile
  value is not a valid dict (type=type_error.dict)

geometry -> partitionFile
  value is not a valid dict (type=type_error.dict)

Where can we catch this validation error and improve the message?

arthurvd commented 2 years ago

Duplicate of #92.