-
Notifications
You must be signed in to change notification settings - Fork 34
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
Feat/validate json #69
Feat/validate json #69
Conversation
geojson_pydantic/geometries.py
Outdated
def validate(cls, value): | ||
try: | ||
return cls(**json.loads(value)) | ||
except Exception: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the goal of this except block to catch errors on json.loads
? If so I think the except block should be more narrowly scoped to TypeError
instead of Exception
. As written we risk swallowing validation errors if JSON is parseable by json.loads
. In general I think this method should separate parsing of value
from validation of value
by passing into cls
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As written we risk swallowing validation errors if JSON is parseable by json.loads
Oh yes, you are right, I will update to fix this issue.
value = json.loads(value) | ||
except TypeError: | ||
try: | ||
return cls(**value.dict()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whats the reason to check for .dict()
attribute here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because we can create a GeometryCollection
with Geometry
as arguments, which has a List[Geometry]
as attribute (Geometry = Union[Point, MultiPoint, LineString, MultiLineString, Polygon, MultiPolygon].
)
The validation of the geometries: List[Geometry]
attribute is run with an object of type Geometry
. Such object cannot be casted into a sublass of _GeometryBase itself, but since it's a subclass to Pydantic BaseModel
, we can just call .dict
to get the value as a dict.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And I prefer the EAFP principle instead of LYBL that would had required a check of instance of BaseModel
to validate such object.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool that makes sense to me, thanks for the explanation!
@vincentsarago this LGTM |
@Vikka I don't seems to be able to push to your branch
🤷♂️ |
You should be able to do it now. |
🙏 thanks for the PR and nice explanation @Vikka |
What I am changing
How I did it
_GeometryBase
base class and loads the value.How you can test it
<GeometryType>.validate(<json geojson formatted>)
, e.g.:Point.validate('{"type": "Point", "coordinates": (1, 2)}')
return a geojson_pydantic.Point object.Related Issues