diff --git a/src/covjson_pydantic/ndarray.py b/src/covjson_pydantic/ndarray.py index 1e065b7..6906335 100644 --- a/src/covjson_pydantic/ndarray.py +++ b/src/covjson_pydantic/ndarray.py @@ -23,7 +23,7 @@ class DataType(str, Enum): class NdArray(CovJsonBaseModel, Generic[NdArrayTypeT], extra="allow"): type: Literal["NdArray"] = "NdArray" - dataType: DataType = DataType.float # noqa: N815 + dataType: DataType # noqa: N815 axisNames: Optional[List[str]] = None # noqa: N815 shape: Optional[List[int]] = None values: List[Optional[NdArrayTypeT]] = None @@ -31,6 +31,9 @@ class NdArray(CovJsonBaseModel, Generic[NdArrayTypeT], extra="allow"): @model_validator(mode="after") def check_data_type(self): t = typing.get_args(self.model_fields["values"].annotation)[0] + if t == typing.Optional[NdArrayTypeT]: + given_type = self.dataType.name if isinstance(self.dataType, DataType) else "" + raise ValueError(f"No NdArray type given, please specify as NdArray[{given_type}]") if self.dataType == DataType.float and not t == typing.Optional[float]: raise ValueError("dataType and NdArray type must both be float.") if self.dataType == DataType.str and not t == typing.Optional[str]: diff --git a/tests/test_coverage.py b/tests/test_coverage.py index 9f3a7cc..57447ce 100644 --- a/tests/test_coverage.py +++ b/tests/test_coverage.py @@ -69,6 +69,7 @@ def test_happy_cases(file_name, object_type): ("mixed-type-axes.json", Axes, r"Input should be a valid number"), ("mixed-type-axes-2.json", Axes, r"Input should be a valid string"), ("mixed-type-ndarray-1.json", NdArray[float], r"Input should be a valid number"), + ("mixed-type-ndarray-1.json", NdArray, r"No NdArray type given, please specify as NdArray\[float\]"), ("mixed-type-ndarray-2.json", NdArray[str], r"dataType and NdArray type must both be float"), ("mixed-type-ndarray-3.json", NdArray[int], r"Input should be a valid integer"), ("mixed-type-ndarray-3.json", NdArray[float], r"dataType and NdArray type must both be integer"), diff --git a/tests/test_data/coverage-mixed-type-ndarray.json b/tests/test_data/coverage-mixed-type-ndarray.json index 5bce9cb..41e6f5d 100644 --- a/tests/test_data/coverage-mixed-type-ndarray.json +++ b/tests/test_data/coverage-mixed-type-ndarray.json @@ -50,6 +50,14 @@ "en": "integer" } } + }, + "null-parameter": { + "type": "Parameter", + "observedProperty": { + "label": { + "en": "null" + } + } } }, "ranges": { @@ -86,7 +94,7 @@ ], "values": [ null, - 62, + 62.0, 63.136801411019825, 63.56289242989419, 63.98448554858814, @@ -110,6 +118,24 @@ 1, 2 ] + }, + "null-parameter": { + "type": "NdArray", + "dataType": "integer", + "axisNames": [ + "x", + "y", + "t" + ], + "shape": [ + 1, + 1, + 2 + ], + "values": [ + null, + null + ] } }, "extra:extra": "extra fields allowed"