-
Notifications
You must be signed in to change notification settings - Fork 149
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
Support kwargs in marshmallow validator #516
Support kwargs in marshmallow validator #516
Conversation
Is this a duplicate of #515? If you want to keep improving your PR you can add commit to your branch. |
Hi @Natim, no it's not a duplicate; #515 fixes a compatibility issue with marshmallow v.3.x (which prohibited its use through cornice's marshmallow helper), this one tries to add some functionality to marshmallow validator, so that one can exploit many more of its features (as I just saw it was also requested as a feature in #501 around 7 months ago). I'll have to see what broke things in the tests and see if I can fix it. |
Ok thank you for the clarification, I have to say I don't know much about marshmallow support in Cornice. |
Np, me neither :). In terms of failing tests, as I saw, I need to add some tests to keep coverage at 100%. Will do it later when I find some free time. |
OK, I've added some comments in validators/_marshmallow.py of how it now supports schema_kwargs variable from views. I've replaced one of the existing tests that used an extra schema that added many=True in its constructor, with one that uses the new functionality (ie passing |
If this PR gets accepted, we should probably update cornice documentation to include this functionality. Not sure if the docs (https://cornice.readthedocs.io) are maintained this repo, if so I have no problem of updating the relevant section. |
Yes the docs are there: https://github.com/Cornices/cornice/tree/master/docs |
What's the status of this? Also, could you please add a CHANGELOG entry and yourself to the CONTRIBUTORS? |
Hi, I need to check if I've included the changes suggested by @Natim, push them in case I haven't done so, and I think we're done with it. And as you suggested, I should update the CHANGELOG and the CONTRIBUTORS files accordingly. |
…yword arguments on marshmallow schema instantiation
537cba5
to
3d46848
Compare
OK, I've updated @Natim, when trying to pass your version of the code, many tests are failing. Here's the error's I'm getting in one of such failing test:
Here's the code I've tried: @m_group_signup.post(
schema=MSignupSchema, schema_kwargs={'many': True},
validators=(marshmallow_body_validator,))
def m_group_signup_post(request):
return {'data': request.validated} Not sure why it happens -your suggestion seems very reasonable to me too-, but unless you want to dig it any futher, I suggest we keep the current version that's passing the tests - and if you're cool with that, obviously. |
Hi there (@Natim, @leplatrem), Maybe it's time for somebody to review and accept the change? |
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.
It looks good to me.
Although I think we should clarify our API regarding schema instance versus class + kwargs. See for example #523
Co-Authored-By: Mathieu Leplatre <[email protected]>
Co-Authored-By: Mathieu Leplatre <[email protected]>
cornice/validators/_marshmallow.py
Outdated
# see if the user wants to set any keyword arguments for their schema | ||
schema_kwargs = {} | ||
if 'schema_kwargs' in kwargs: | ||
schema_kwargs = kwargs.get('schema_kwargs', {}) |
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.
Sorry this code suggestion thing didn't work as I expected. You can remove the above lines
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.
Sorry, you lost me in this. The truth is that I accepted this change without seeing it too deeply, I thought it was in a different spot. Now I'm a bit confused, has it been merged with my code or not?
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.
Just that these 3 lines can be replaced by the last one
schema_kwargs = kwargs.get('schema_kwargs', {})
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 it OK now? I'm not familiar with these github's features (merging suggested changes and editing files within github), that's why I'm asking.
So, I've edited the file within github to only contain schema_kwargs = kwargs.get('schema_kwargs', {})
and I suppose it's OK now, but I'm not sure I saw the checks running again.
EDIT:
I've clicked on the checks and the logs show they have run successfully after I made the last changes within github, so I suppose we're OK.
Hi @leplatrem and thanks for the comments and suggestions. In terms of class vs instance, I haven't changed anything in the current implementation. Marshmallow is different from colander in this matter. It allows you to reuse the same validator class and tailor it appropriately upon instantiation (using constructor's args/kwargs), which is one of its very strengths. So, despite the fact that the provided PR is not very "elegant" (having a Once this PR (and #515) are complete and accepted, I'll also update the documentation in order for people to know they can use it. |
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.
👏 👏
With this patch, keyword arguments are supported when instantiating the marshmallow schema (#511).
This is an example of how to use it:
So, if someone passes schema_kwargs dictionary in a marshmallow validator, it is expanded in the schema's constructor when it is instantiated.
Pinging @ergo for a second opinion.