Skip to content

Commit

Permalink
Merge pull request #258 from RWTH-EBC/226-Add-geojson-validation
Browse files Browse the repository at this point in the history
feat: add geojson validation
  • Loading branch information
djs0109 authored Mar 19, 2024
2 parents c528db0 + 34ffafd commit 8f4a9a5
Show file tree
Hide file tree
Showing 4 changed files with 366 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- add function to override the existing entity ([#232 ](https://github.com/RWTH-EBC/FiLiP/pull/232 ))
- fix: include headers in some requests ([#250](https://github.com/RWTH-EBC/FiLiP/issues/250))
- feat: make context-entity more customizable ([#225](https://github.com/RWTH-EBC/FiLiP/issues/225))
- feat: add geojson support to context-entity ([#226](https://github.com/RWTH-EBC/FiLiP/issues/226))

BREAKING CHANGE:
- feat: make context-entity more customizable ([#225](https://github.com/RWTH-EBC/FiLiP/issues/225)) enforces stricter type validation as before. This might lead to errors in your code if you are not using the correct types. Please check the documentation for the correct types.
Expand Down
56 changes: 51 additions & 5 deletions filip/models/ngsi_v2/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@
import json

from aenum import Enum
from geojson_pydantic import (
Point,
MultiPoint,
LineString,
MultiLineString,
Polygon,
MultiPolygon,
Feature,
FeatureCollection,
)
from pydantic import (
field_validator,
model_validator,
Expand All @@ -13,8 +23,7 @@
Field,
model_serializer,
SerializationInfo,
FieldValidationInfo,
ValidationInfo
ValidationInfo,
)

from typing import Union, Optional, Pattern, List, Dict, Any
Expand Down Expand Up @@ -215,7 +224,7 @@ class Metadata(BaseModel):
)

@field_validator("value")
def validate_value(cls, value, info: FieldValidationInfo):
def validate_value(cls, value, info: ValidationInfo):
assert json.dumps(value), "metadata not serializable"

if info.data.get("type").casefold() == "unit":
Expand Down Expand Up @@ -424,8 +433,7 @@ def validate_value_type(cls, value, info: ValidationInfo):
if type_ == DataType.ARRAY:
if isinstance(value, list):
return value
raise TypeError(f"{type(value)} does not match "
f"{DataType.ARRAY}")
raise TypeError(f"{type(value)} does not match " f"{DataType.ARRAY}")
# allows dict and BaseModel as object
if type_ == DataType.OBJECT:
if isinstance(value, dict):
Expand All @@ -437,6 +445,44 @@ def validate_value_type(cls, value, info: ValidationInfo):
raise TypeError(
f"{type(value)} does not match " f"{DataType.OBJECT}"
)
# allows geojson as structured value
if type_ == DataType.GEOJSON:
if isinstance(
value,
(
Point,
MultiPoint,
LineString,
MultiLineString,
Polygon,
MultiPolygon,
Feature,
FeatureCollection,
),
):
return value

if isinstance(value, dict):
_geo_json_type = value.get("type", None)
if _geo_json_type == "Point":
return Point(**value)
elif _geo_json_type == "MultiPoint":
return MultiPoint(**value)
elif _geo_json_type == "LineString":
return LineString(**value)
elif _geo_json_type == "MultiLineString":
return MultiLineString(**value)
elif _geo_json_type == "Polygon":
return Polygon(**value)
elif _geo_json_type == "MultiPolygon":
return MultiPolygon(**value)
elif _geo_json_type == "Feature":
return Feature(**value)
elif _geo_json_type == "FeatureCollection":
return FeatureCollection(**value)
raise TypeError(f"{type(value)} does not match "
f"{DataType.GEOJSON}")

# allows list, dict and BaseModel as structured value
if type_ == DataType.STRUCTUREDVALUE:
if isinstance(value, (dict, list)):
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ requests~=2.31.0
python-dotenv>=0.21.0
pydantic~=2.5.2
pydantic-settings~=2.0.0
geojson_pydantic~=1.0.2
aenum~=3.1.15
pathlib~=1.0.1
regex~=2023.10.3
Expand Down
Loading

0 comments on commit 8f4a9a5

Please sign in to comment.