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

Validate illegal fields are not present under attributes and relationships #83

Merged
merged 6 commits into from Nov 20, 2019

Conversation

CasperWA
Copy link
Member

Fixes #22

When/If we get to pydantic v1.0, root_validators can be used for this instead, see here.

I have tried simply having the validator without specifying the fields in the models, but then the validator will never be called - for some reason. So by specifying the illegal fields as fields it is possible to have a working validator for them that simply raises immediately.

This is validated for Attributes and Relationships.

When/If we get to pydantic v1.0, root_validators can be used for this
instead.
@CasperWA CasperWA added the schema Concerns the schema models label Nov 14, 2019
@CasperWA CasperWA added this to In progress in Road to optimade-python-tools 1.0 via automation Nov 14, 2019
@codecov
Copy link

codecov bot commented Nov 14, 2019

Codecov Report

Merging #83 into master will decrease coverage by 0.36%.
The diff coverage is 91.66%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #83      +/-   ##
==========================================
- Coverage   84.32%   83.96%   -0.37%     
==========================================
  Files          35       35              
  Lines        2099     2058      -41     
==========================================
- Hits         1770     1728      -42     
- Misses        329      330       +1
Impacted Files Coverage Δ
optimade/models/structures.py 81.3% <100%> (-2.04%) ⬇️
optimade/models/jsonapi.py 87.95% <90.9%> (-0.21%) ⬇️
optimade/models/optimade_json.py 84% <0%> (-2.21%) ⬇️
optimade/validator/validator.py 62.8% <0%> (-1.05%) ⬇️
optimade/filtertransformers/mongo.py 75.57% <0%> (-0.9%) ⬇️
optimade/filtertransformers/tests/test_django.py 88.23% <0%> (-0.66%) ⬇️
optimade/models/links.py 90.47% <0%> (-0.44%) ⬇️
optimade/server/mappers/structures.py 93.93% <0%> (-0.35%) ⬇️
... and 8 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7aa2388...b8a1574. Read the comment docs.

Copy link
Member

@ml-evs ml-evs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also LGTM, couple of comments below

class Config:
extra = "allow"

@validator("relationships", "links", "id", "type")
def check_illegal_attributes_fields(cls, v):
raise AssertionError(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I try to add a test for this (see my commit), this gets raised as an AssertionError rather than a ValidationError (as I'd expect). Do we want to make this return a validation error instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. But isn't that a bit weird? Not the change, but that you're not catching it. We should have an AssertionError in the structure model as well. Do you catch that one?
Should I change it to ValueError or a ValidationError?

Copy link
Member

@ml-evs ml-evs Nov 15, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the point is that pydantic is meant to internally catch AssertionErrors (it worked with just assert statements right?) so I'm a bit confused why this doesn't work (see https://pydantic-docs.helpmanual.io/usage/validators/). ValueError would probably be the next most appropriate?

(The point being this would break any reporting of further errors in validation)


@validator("id", "type")
def check_illegal_relationships_fields(cls, v):
raise AssertionError('"id", "type" MUST NOT be fields under relationships')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also change all these other validators to ValueError?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To speed things up, I'll accept as is and you can merge if you want. If you change it just re-request my review and I'll accept.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we should? Since we didn't encounter this before, it must mean that we are not actually testing this validator?

@CasperWA CasperWA merged commit 0b26bc3 into master Nov 20, 2019
Road to optimade-python-tools 1.0 automation moved this from In progress to Done Nov 20, 2019
@CasperWA CasperWA deleted the fix_22_validate_attribute_fields branch November 20, 2019 18:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
schema Concerns the schema models
Development

Successfully merging this pull request may close these issues.

server.jsonapi has no patternProperties
2 participants