RWTH-EBC / FiLiP

FIWARE Library for Python (FiLiP) to work with FIWARE API
BSD 3-Clause "New" or "Revised" License
23 stars 13 forks source link

Add geojson validation #226

Closed tstorek closed 6 months ago

tstorek commented 8 months ago

Is your feature request related to a problem? Please describe. using geo:json and geo specific requests is a powerful feature of FIWARE. Currently, there is no validation for "geo:json"-type. However, luckily there is already a pydantic library for geo:json.

Describe the solution you'd like

  1. Define "geo:json" Attribute.
  2. Enforce validation by restricting the value to the models from geojson-pydantic

Describe alternatives you've considered Leave it to the user.

Additional context


from geojson_pydantic import (
    Point,
    MultiPoint,
    LineString,
    MultiLineString,
    Polygon,
    MultiPolygon,
    Feature,
    FeatureCollection,
)

class GeoJsonAttribute(ContextAttribute):
    """
    https://fiware-orion.readthedocs.io/en/master/orion-api.html#geospatial-properties-of-entities
    """
    type: Literal["geo:json"] = FieldInfo.merge_field_infos(
        CustomContextAttribute.model_fields["type"],
        title="Type of the GeoJSON attribute",
        default="geo:json",
        frozen=True,
        validate_default=True,
        description="Type of GeoJSON attribute.",
    )
    value: Union[
        Point,
        MultiPoint,
        LineString,
        MultiLineString,
        Polygon,
        MultiPolygon,
        Feature,
        FeatureCollection,
    ] = FieldInfo.merge_field_infos(
        CustomContextAttribute.model_fields["value"],
        title="GeoJSON object",
        description="GeoJSON object. Allowed types are `Point`, `MultiPoint`, "
        "`LineString`, `MultiLineString`, `Polygon`, `MultiPolygon`, "
        "`Feature` and `FeatureCollection`",
        examples=[{"type": "Point", "coordinates": [125.6, 10.1]}],
    )
github-actions[bot] commented 8 months ago

Branch 226-Add-geojson-validation created!