Verbose validation error is difficult to understand
Closed this issue · 3 comments
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.
@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
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.
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.