Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Verbose validation error is difficult to understand #93

Closed
svaccari opened this issue Jan 26, 2023 · 3 comments
Closed

Verbose validation error is difficult to understand #93

svaccari opened this issue Jan 26, 2023 · 3 comments

Comments

@svaccari
Copy link

Hi, I was validating this Polygon that has a linear rings with different start/end coordinates:

{
    "type": "Feature",
    "geometry": {
        "type": "Polygon",
        "coordinates": [
            [
                [-55.9947406591177, -9.26104045526505],
                [-55.9976752102375, -9.266589696568962],
                [-56.00200328975916, -9.264041751931352],
                [-55.99899921566248, -9.257935213034594],
                [-55.99477406591177, -9.26103945526505],
            ]
        ],
    },
}

And I received the following message:

18 validation errors for Feature
geometry -> type
  unexpected value; permitted: 'Point' (type=value_error.const; given=Polygon; permitted=['Point'])
geometry -> coordinates
  wrong tuple length 1, expected 2 (type=value_error.tuple.length; actual_length=1; expected_length=2)
geometry -> coordinates
  wrong tuple length 1, expected 3 (type=value_error.tuple.length; actual_length=1; expected_length=3)
geometry -> type
  unexpected value; permitted: 'MultiPoint' (type=value_error.const; given=Polygon; permitted=['MultiPoint'])
geometry -> coordinates -> 0
  wrong tuple length 5, expected 2 (type=value_error.tuple.length; actual_length=5; expected_length=2)
geometry -> coordinates -> 0
  wrong tuple length 5, expected 3 (type=value_error.tuple.length; actual_length=5; expected_length=3)
geometry -> type
  unexpected value; permitted: 'LineString' (type=value_error.const; given=Polygon; permitted=['LineString'])
geometry -> coordinates
  ensure this value has at least 2 items (type=value_error.list.min_items; limit_value=2)
geometry -> type
  unexpected value; permitted: 'MultiLineString' (type=value_error.const; given=Polygon; permitted=['MultiLineString'])
geometry -> coordinates
  All linear rings have the same start and end coordinates (type=value_error)
geometry -> type
  unexpected value; permitted: 'MultiPolygon' (type=value_error.const; given=Polygon; permitted=['MultiPolygon'])
geometry -> coordinates -> 0 -> 0
  ensure this value has at least 4 items (type=value_error.list.min_items; limit_value=4)
geometry -> coordinates -> 0 -> 1
  ensure this value has at least 4 items (type=value_error.list.min_items; limit_value=4)
geometry -> coordinates -> 0 -> 2
  ensure this value has at least 4 items (type=value_error.list.min_items; limit_value=4)
geometry -> coordinates -> 0 -> 3
  ensure this value has at least 4 items (type=value_error.list.min_items; limit_value=4)
geometry -> coordinates -> 0 -> 4
  ensure this value has at least 4 items (type=value_error.list.min_items; limit_value=4)
geometry -> type
  unexpected value; permitted: 'GeometryCollection' (type=value_error.const; given=Polygon; permitted=['GeometryCollection'])
geometry -> geometries
  field required (type=value_error.missing)

However this does not happen if Feature is omitted:

{
    "type": "Polygon",
    "coordinates": [
        [
            [-55.9947406591177, -9.26104045526505],
            [-55.9976752102375, -9.266589696568962],
            [-56.00200328975916, -9.264041751931352],
            [-55.99899921566248, -9.257935213034594],
            [-55.99477406591177, -9.26103945526505],
        ]
    ],
}

Result:

1 validation error for Polygon
coordinates
  All linear rings have the same start and end coordinates (type=value_error)

Can you look into that?
Thanks.

@vincentsarago
Copy link
Member

vincentsarago commented Jan 26, 2023

@svaccari the first verbose error is because Pydantic will try to check all the geometry types instead of focusing on just the Polygon. Maybe there is something to explore there

edit:

I think we should use https://github.com/developmentseed/geojson-pydantic/blob/main/geojson_pydantic/geometries.py#L246 in https://github.com/developmentseed/geojson-pydantic/blob/main/geojson_pydantic/features.py#L29-L34

@eseglem
Copy link
Collaborator

eseglem commented Jan 27, 2023

Depends on #94 (types as Literals). But this is 90% solved by the following:

from typing_extensions import Annotated
...
Geometry = Annotated[
    Union[Point, MultiPoint, LineString, MultiLineString, Polygon, MultiPolygon],
    Field(discriminator="type"),
]

The error would then just be:

pydantic.error_wrappers.ValidationError: 3 validation errors for Feature
geometry -> Polygon -> coordinates
  All linear rings have the same start and end coordinates (type=value_error)
geometry -> type
  unexpected value; permitted: 'GeometryCollection' (type=value_error.const; given=Polygon; permitted=('GeometryCollection',))
geometry -> geometries
  field required (type=value_error.missing)

I haven't quite figured out getting rid of the GeometryCollection one yet. But at least its significantly improved.

@eseglem
Copy link
Collaborator

eseglem commented Jan 27, 2023

Took a bunch of iterations, but got it:

class Feature(GenericModel, Generic[Geom, Props]):
    ...
    geometry: Annotated[Union[Geom, None], Field(..., discriminator="type")]

And then you get

pydantic.error_wrappers.ValidationError: 1 validation error for Feature
geometry -> Union[Point, MultiPoint, LineString, MultiLineString, Polygon, MultiPolygon] -> Polygon -> coordinates
  All linear rings have the same start and end coordinates (type=value_error)

Edit: Updated to be required field.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants