Skip to content

Commit 8f4a9a5

Browse files
authored
Merge pull request #258 from RWTH-EBC/226-Add-geojson-validation
feat: add geojson validation
2 parents c528db0 + 34ffafd commit 8f4a9a5

File tree

4 files changed

+366
-12
lines changed

4 files changed

+366
-12
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- add function to override the existing entity ([#232 ](https://github.com/RWTH-EBC/FiLiP/pull/232 ))
99
- fix: include headers in some requests ([#250](https://github.com/RWTH-EBC/FiLiP/issues/250))
1010
- feat: make context-entity more customizable ([#225](https://github.com/RWTH-EBC/FiLiP/issues/225))
11+
- feat: add geojson support to context-entity ([#226](https://github.com/RWTH-EBC/FiLiP/issues/226))
1112

1213
BREAKING CHANGE:
1314
- 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.

filip/models/ngsi_v2/base.py

+51-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@
44
import json
55

66
from aenum import Enum
7+
from geojson_pydantic import (
8+
Point,
9+
MultiPoint,
10+
LineString,
11+
MultiLineString,
12+
Polygon,
13+
MultiPolygon,
14+
Feature,
15+
FeatureCollection,
16+
)
717
from pydantic import (
818
field_validator,
919
model_validator,
@@ -13,8 +23,7 @@
1323
Field,
1424
model_serializer,
1525
SerializationInfo,
16-
FieldValidationInfo,
17-
ValidationInfo
26+
ValidationInfo,
1827
)
1928

2029
from typing import Union, Optional, Pattern, List, Dict, Any
@@ -215,7 +224,7 @@ class Metadata(BaseModel):
215224
)
216225

217226
@field_validator("value")
218-
def validate_value(cls, value, info: FieldValidationInfo):
227+
def validate_value(cls, value, info: ValidationInfo):
219228
assert json.dumps(value), "metadata not serializable"
220229

221230
if info.data.get("type").casefold() == "unit":
@@ -424,8 +433,7 @@ def validate_value_type(cls, value, info: ValidationInfo):
424433
if type_ == DataType.ARRAY:
425434
if isinstance(value, list):
426435
return value
427-
raise TypeError(f"{type(value)} does not match "
428-
f"{DataType.ARRAY}")
436+
raise TypeError(f"{type(value)} does not match " f"{DataType.ARRAY}")
429437
# allows dict and BaseModel as object
430438
if type_ == DataType.OBJECT:
431439
if isinstance(value, dict):
@@ -437,6 +445,44 @@ def validate_value_type(cls, value, info: ValidationInfo):
437445
raise TypeError(
438446
f"{type(value)} does not match " f"{DataType.OBJECT}"
439447
)
448+
# allows geojson as structured value
449+
if type_ == DataType.GEOJSON:
450+
if isinstance(
451+
value,
452+
(
453+
Point,
454+
MultiPoint,
455+
LineString,
456+
MultiLineString,
457+
Polygon,
458+
MultiPolygon,
459+
Feature,
460+
FeatureCollection,
461+
),
462+
):
463+
return value
464+
465+
if isinstance(value, dict):
466+
_geo_json_type = value.get("type", None)
467+
if _geo_json_type == "Point":
468+
return Point(**value)
469+
elif _geo_json_type == "MultiPoint":
470+
return MultiPoint(**value)
471+
elif _geo_json_type == "LineString":
472+
return LineString(**value)
473+
elif _geo_json_type == "MultiLineString":
474+
return MultiLineString(**value)
475+
elif _geo_json_type == "Polygon":
476+
return Polygon(**value)
477+
elif _geo_json_type == "MultiPolygon":
478+
return MultiPolygon(**value)
479+
elif _geo_json_type == "Feature":
480+
return Feature(**value)
481+
elif _geo_json_type == "FeatureCollection":
482+
return FeatureCollection(**value)
483+
raise TypeError(f"{type(value)} does not match "
484+
f"{DataType.GEOJSON}")
485+
440486
# allows list, dict and BaseModel as structured value
441487
if type_ == DataType.STRUCTUREDVALUE:
442488
if isinstance(value, (dict, list)):

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ requests~=2.31.0
22
python-dotenv>=0.21.0
33
pydantic~=2.5.2
44
pydantic-settings~=2.0.0
5+
geojson_pydantic~=1.0.2
56
aenum~=3.1.15
67
pathlib~=1.0.1
78
regex~=2023.10.3

0 commit comments

Comments
 (0)