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

Feat/validate json #69

Merged
merged 4 commits into from
Jun 8, 2022

Conversation

p1-dta
Copy link
Contributor

@p1-dta p1-dta commented Jun 5, 2022

What I am changing

How I did it

  • add the self validation method for the _GeometryBase base class and loads the value.

How you can test it

  • f0cc92f add few tests for this feature,
  • <GeometryType>.validate(<json geojson formatted>), e.g.: Point.validate('{"type": "Point", "coordinates": (1, 2)}') return a geojson_pydantic.Point object.

Related Issues

def validate(cls, value):
try:
return cls(**json.loads(value))
except Exception:
Copy link
Contributor

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.

Copy link
Contributor Author

@p1-dta p1-dta Jun 7, 2022

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())
Copy link
Contributor

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?

Copy link
Contributor Author

@p1-dta p1-dta Jun 7, 2022

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.

Copy link
Contributor Author

@p1-dta p1-dta Jun 7, 2022

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.

Copy link
Contributor

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!

@geospatial-jeff
Copy link
Contributor

@vincentsarago this LGTM

@vincentsarago
Copy link
Member

@Vikka I don't seems to be able to push to your branch

git push vikka HEAD:feat/validate_json 
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Delta compression using up to 8 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 498 bytes | 498.00 KiB/s, done.
Total 4 (delta 2), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
To https://github.com/Vikka/geojson-pydantic
 ! [remote rejected] HEAD -> feat/validate_json (permission denied)

🤷‍♂️

@p1-dta
Copy link
Contributor Author

p1-dta commented Jun 8, 2022

@vincentsarago

@Vikka I don't seems to be able to push to your branch

You should be able to do it now.

@vincentsarago vincentsarago merged commit 8096bd3 into developmentseed:master Jun 8, 2022
@vincentsarago
Copy link
Member

🙏 thanks for the PR and nice explanation @Vikka

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

Successfully merging this pull request may close these issues.

None yet

3 participants